ByteStore. Builds, tests and edge cases
What ByteStore is, what tests establish, and what the Rocq model says.
This series verifies one small C++ program end to end: ByteStore.
Answers. What ByteStore is, what tests establish, and what the Rocq model says.
1. ByteStore
ByteStore’s C++ implementation is the class CheckedStore in checked_store.hpp. The class, in full:
#include <cstddef>
#include <cstdint>
#include <limits>
#include <map>
#include <optional>
#include <vector>
// Each identity is meaningful only within the live store that issued it.
class CheckedStore {
public:
using Identity = std::uint64_t;
using Byte = std::uint8_t;
explicit CheckedStore(Identity identity_limit = std::numeric_limits<Identity>::max())
: identity_limit_(identity_limit) {}
CheckedStore(const CheckedStore&) = delete;
CheckedStore& operator=(const CheckedStore&) = delete;
CheckedStore(CheckedStore&&) = delete;
CheckedStore& operator=(CheckedStore&&) = delete;
[[nodiscard]] std::optional<Identity> allocate(std::size_t length) {
if (next_identity_ >= identity_limit_) {
return std::nullopt;
}
// Every live key is below this identity. Advance only after insertion succeeds.
const Identity id = next_identity_;
buffers_.try_emplace(id, length, Byte{0});
++next_identity_;
return id;
}
[[nodiscard]] std::optional<Byte> read(Identity id, std::size_t offset) const {
const auto entry = buffers_.find(id);
if (entry == buffers_.end() || offset >= entry->second.size()) {
return std::nullopt;
}
return entry->second.at(offset);
}
[[nodiscard]] bool write(Identity id, std::size_t offset, Byte value) {
const auto entry = buffers_.find(id);
if (entry == buffers_.end() || offset >= entry->second.size()) {
return false;
}
entry->second.at(offset) = value;
return true;
}
[[nodiscard]] bool release(Identity id) {
return buffers_.erase(id) != 0;
}
private:
const Identity identity_limit_;
Identity next_identity_ = 0;
std::map<Identity, std::vector<Byte>> buffers_;
};
2. Build and run
- Run
run_cpp.sh: clang++ with-std=c++17 -Wall -Wextra -Wpedantic -Wconversion -Werror. Build 1:-O2 -DNDEBUG. Build 2 uses ASan and UBSan with-fno-sanitize-recover=all. Build 2 is skipped only when the sanitizer probe itself does not build. - Tests use an explicit
CHECKmacro. The macro stays active underNDEBUGand exits nonzero on failure. - Tests cover selected executions only. Unexecuted states are the model’s job, section 4.
3. Edge cases. Predict from SPEC.md, then run
- Zero length buffer. Valid. No offset is accessible. Every read and write is rejected, including
offset = SIZE_MAX. offset == length. Rejected, store unchanged. A maximal offset is also rejected.- Released ID. Read, write, and release are all rejected. A later allocation gets a fresh ID: with limit 3, release ID 0 and the next allocation is 2.
- Exhaustion. At the limit, allocation fails closed. The counter stays put and existing buffers stay untouched. With limit 0, allocation always fails.
- Allocation exception. An oversize length throws
length_errororbad_alloc. ID 1 stays available. The next successful allocation returns 1. - Copy semantics.
static_asserts state that copy and move are non-constructible and non-assignable.
4. Model.v read closely. 332 lines
Excerpt from Model.v: lookup, allocate, write, and release, the definitions the theorems below state properties of.
Steps: allocate adds ID 3 and next_id becomes 4; a write changes one byte in ID 0; release turns ID 2 into None; a write to ID 2 is rejected and leaves every slot unchanged.
Model.v, core definitions
Definition lookup (s : store) (id : nat) : option buffer :=
match nth_error (slots s) id with
| Some entry => entry
| None => None
end.
Definition allocate (s : store) (size : nat) : option nat * store :=
if next_id s <? limit s then
(Some (next_id s), append_buffer s (repeat x00 size))
else (None, s).
Definition write (s : store) (id offset : nat) (value : byte) : bool * store :=
match lookup s id with
| Some b =>
if offset <? length b then
(true, set_slot s id (Some (replace_at offset value b)))
else (false, s)
| None => (false, s)
end.
Definition release (s : store) (id : nat) : bool * store :=
match lookup s id with
| Some _ => (true, set_slot s id None)
| None => (false, s)
end.
Reading Rocq: Definition computes, Theorem claims, Qed means the kernel checked the proof.
write_live_in_bounds
Theorem write_live_in_bounds s id offset value after :
write s id offset value = (true, after) ->
exists b, lookup s id = Some b /\ offset < length b /\
after = set_slot s id (Some (replace_at offset value b)).
rejected_write_preserves_store
Theorem rejected_write_preserves_store s id offset value after :
write s id offset value = (false, after) -> after = s.
write_changes_one_byte
Theorem write_changes_one_byte s id offset value after :
write s id offset value = (true, after) ->
read after id offset = Some value /\
forall j k, j <> id \/ k <> offset -> read after j k = read s j k.
released_handle_stays_invalid
Theorem released_handle_stays_invalid s id after ops offset value :
release s id = (true, after) ->
let final := run after ops in
read final id offset = None /\ write final id offset value = (false, final) /\
release final id = (false, final).
allocate_available·allocate_exhausted
Theorem allocate_available s size :
next_id s < limit s ->
allocate s size = (Some (next_id s), append_buffer s (repeat x00 size)).
Theorem allocate_exhausted s size :
limit s <= next_id s -> allocate s size = (None, s).
allocation_fresh·allocation_ids_strictly_increase
Theorem allocation_fresh s size id after :
allocate s size = (Some id, after) ->
id = next_id s /\ id < limit s /\ next_id after = S id /\
lookup s id = None /\ lookup after id = Some (repeat x00 size).
Theorem allocation_ids_strictly_increase s size id after ops size' id' final :
allocate s size = (Some id, after) ->
allocate (run after ops) size' = (Some id', final) ->
id < id'.
read_live_in_bounds
Theorem read_live_in_bounds s id offset value :
read s id offset = Some value ->
exists b, lookup s id = Some b /\ offset < length b.
absent_release_preserves_store
Theorem absent_release_preserves_store s id :
lookup s id = None -> release s id = (false, s).
Sources: checked_store.hpp · tests.cpp · run_cpp.sh · Model.v.