- Correct stale references and headline-path wording in READMEs and Lean docstrings - Document build steps, comparator requirements, sweep scope, and coverage - Unify Apache headers across Lean and C++ sources - Add a bwrap-free axiom audit for the headline theorems - Add a Gitea workflow running lake build, the axiom audit, C++ tests, and coverage - Match LLVM coverage tool versions to the compiler and recreate stale build dirs
196 lines
6.0 KiB
C++
196 lines
6.0 KiB
C++
// Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||
// Authors: FormalConflictRange contributors
|
||
|
||
// Deterministic, readable-by-construction instance generation shared by the
|
||
// differential fuzzer and the benchmarks.
|
||
//
|
||
// A generated pair `(r, res)` always satisfies `Readable r res`: we build a
|
||
// sorted, duplicate-free database, resolve the two selectors against it, and
|
||
// report either the complete traversal or a nonempty prefix of it.
|
||
#pragma once
|
||
|
||
#include <algorithm>
|
||
#include <cstddef>
|
||
#include <cstdint>
|
||
#include <utility>
|
||
#include <vector>
|
||
|
||
#include "../src/model.h"
|
||
|
||
namespace cr::gen {
|
||
|
||
struct Bytes {
|
||
const std::uint8_t* data = nullptr;
|
||
std::size_t size = 0;
|
||
std::size_t pos = 0;
|
||
|
||
std::uint8_t u8() {
|
||
if (size == 0) return 0;
|
||
std::uint8_t v = data[pos % size];
|
||
++pos;
|
||
return v;
|
||
}
|
||
std::uint16_t u16() {
|
||
std::uint16_t lo = u8();
|
||
std::uint16_t hi = u8();
|
||
return static_cast<std::uint16_t>(lo | (hi << 8));
|
||
}
|
||
};
|
||
|
||
inline Byte weightedByte(Bytes& b) {
|
||
// Weight the common boundary bytes; otherwise take the raw byte.
|
||
std::uint8_t r = b.u8();
|
||
if (r < 24) return 0;
|
||
if (r < 40) return 1;
|
||
if (r < 56) return 2;
|
||
if (r < 72) return 255;
|
||
return b.u8();
|
||
}
|
||
|
||
inline Key randomKey(Bytes& b, std::size_t maxLen) {
|
||
std::size_t len = b.u8() % (maxLen + 1);
|
||
Key k;
|
||
k.reserve(len);
|
||
for (std::size_t i = 0; i < len; ++i)
|
||
k.push_back(static_cast<char>(weightedByte(b)));
|
||
return k;
|
||
}
|
||
|
||
inline OptKey randomOptKey(Bytes& b, std::size_t maxLen) {
|
||
// One in eight selectors is anchored at `⊤`.
|
||
if ((b.u8() & 7) == 0) return std::nullopt;
|
||
return OptKey{randomKey(b, maxLen)};
|
||
}
|
||
|
||
inline Int randomOffset(Bytes& b) {
|
||
// Bias towards the offsets that decide the interesting boundary cases, but
|
||
// still allow the full [-256, 255] window.
|
||
std::uint8_t pick = b.u8();
|
||
if (pick < 192) {
|
||
static const Int small[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
|
||
return small[pick % 9];
|
||
}
|
||
return static_cast<Int>(b.u16() % 512) - 256;
|
||
}
|
||
|
||
inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
|
||
Int anchor;
|
||
if (!s.key) {
|
||
anchor = static_cast<Int>(db.size()) + s.offset - 1;
|
||
} else if (s.key->empty()) {
|
||
anchor = s.offset - 1;
|
||
} else {
|
||
std::size_t below = 0;
|
||
for (const Key& k : db)
|
||
if (keyLt(k, *s.key)) ++below;
|
||
anchor = static_cast<Int>(below) + s.offset - 1;
|
||
}
|
||
if (anchor < 0) return OptKey{Key{}};
|
||
if (anchor >= static_cast<Int>(db.size())) return std::nullopt;
|
||
return OptKey{db[static_cast<std::size_t>(anchor)]};
|
||
}
|
||
|
||
inline std::vector<Key> inRange(const std::vector<Key>& db, const OptKey& lo,
|
||
const OptKey& hi) {
|
||
std::vector<Key> out;
|
||
for (const Key& k : db) {
|
||
bool geLo = lo.has_value() && !keyLt(k, *lo);
|
||
bool ltHi = !hi || keyLt(k, *hi);
|
||
if (geLo && ltHi) out.push_back(k);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// Owning storage for a generated instance. `RangeRead` and `Result` borrow
|
||
// keys, so the instance keeps the keys alive and rebuilds views on demand.
|
||
struct Instance {
|
||
// Reported keys in traversal order; backing storage for `views`.
|
||
std::vector<Key> keys;
|
||
// Views into `keys`, built by `finish()`.
|
||
std::vector<KeyView> views;
|
||
// Owned selector keys; `read()` borrows them.
|
||
OptKey beginKey;
|
||
OptKey endKey;
|
||
Int beginOffset = 0;
|
||
Int endOffset = 0;
|
||
bool reverse = false;
|
||
bool more = false;
|
||
|
||
Instance() = default;
|
||
// Move-only: `views` borrow from `keys`. Moving a `std::vector` keeps its
|
||
// elements at the same addresses, so the views remain valid; a copy would
|
||
// leave the new instance's views pointing into the source's storage.
|
||
Instance(const Instance&) = delete;
|
||
Instance& operator=(const Instance&) = delete;
|
||
Instance(Instance&&) = default;
|
||
Instance& operator=(Instance&&) = default;
|
||
|
||
// Must be called once `keys` is final; call again after mutating `keys` or
|
||
// after moving keys in by hand.
|
||
void finish() {
|
||
views.clear();
|
||
views.reserve(keys.size());
|
||
for (const Key& k : keys) views.push_back(k);
|
||
}
|
||
|
||
RangeRead read() const {
|
||
RangeRead r;
|
||
r.begin = Selector{optView(beginKey), beginOffset};
|
||
r.end_ = Selector{optView(endKey), endOffset};
|
||
r.reverse = reverse;
|
||
return r;
|
||
}
|
||
|
||
Result result() const { return Result{views, more}; }
|
||
};
|
||
|
||
inline Instance readable(Bytes& b) {
|
||
const std::size_t maxLen = 1 + b.u8() % 16; // key length 0..16
|
||
std::vector<Key> db;
|
||
const std::size_t count = b.u8() % 20; // 0..19 stored keys
|
||
db.reserve(count);
|
||
for (std::size_t i = 0; i < count; ++i) db.push_back(randomKey(b, maxLen));
|
||
std::sort(db.begin(), db.end());
|
||
db.erase(std::unique(db.begin(), db.end()), db.end());
|
||
|
||
Instance inst;
|
||
inst.beginKey = randomOptKey(b, maxLen);
|
||
inst.beginOffset = randomOffset(b);
|
||
inst.endKey = randomOptKey(b, maxLen);
|
||
inst.endOffset = randomOffset(b);
|
||
inst.reverse = (b.u8() & 1) != 0;
|
||
|
||
OptKey lo = resolve(db, Selector{optView(inst.beginKey), inst.beginOffset});
|
||
OptKey hi = resolve(db, Selector{optView(inst.endKey), inst.endOffset});
|
||
std::vector<Key> complete = inRange(db, lo, hi);
|
||
if (inst.reverse) std::reverse(complete.begin(), complete.end());
|
||
|
||
if (!complete.empty() && (b.u8() & 1) != 0) {
|
||
std::size_t m = 1 + b.u8() % complete.size();
|
||
inst.keys.assign(complete.begin(),
|
||
complete.begin() + static_cast<std::ptrdiff_t>(m));
|
||
inst.more = true;
|
||
} else {
|
||
inst.keys = std::move(complete);
|
||
inst.more = false;
|
||
}
|
||
inst.finish();
|
||
return inst;
|
||
}
|
||
|
||
// Range equivalence used by the differential check: any encoding of the empty
|
||
// interval is equivalent to any other; nonempty intervals must match exactly.
|
||
inline bool isEmptyRange(const ConflictRange& c) {
|
||
return !optLt(c.begin, c.end_);
|
||
}
|
||
|
||
inline bool equivalent(const ConflictRange& a, const ConflictRange& b) {
|
||
const bool ea = isEmptyRange(a);
|
||
const bool eb = isEmptyRange(b);
|
||
if (ea || eb) return ea && eb;
|
||
return a.begin == b.begin && a.end_ == b.end_;
|
||
}
|
||
|
||
} // namespace cr::gen
|