okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

waveringanadsv41: a pinned store slot whose read failed is freed only by its last holder, so a window streaming its landed pieces never sees it refilled with another expert3450cddd5

main
5.3 KiB130 linesraw
1// Aligned reads must preserve every matrix byte, including unaligned file offsets and reused slots.
2#include "ds4_store.hpp"
3
4#include <filesystem>
5#include <fstream>
6#include <thread>
7
8using strata::ds4::ExpertStore;
9namespace os = strata::ds4::os;
10
11struct File {
12    std::string path = (std::filesystem::temp_directory_path() /
13        ("strata-store-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()))).string();
14    std::vector<uint8_t> bytes = std::vector<uint8_t>(1 << 20);
15    os::fd_t fd = os::kBadFd, direct = os::kBadFd;
16    File() {
17        uint32_t v = 17;
18        for (auto& b : bytes) { v = v * 1664525u + 1013904223u; b = (uint8_t) (v >> 24); }
19        { std::ofstream out(path, std::ios::binary); out.write((const char*) bytes.data(), bytes.size()); }
20        fd = os::open_par(path);
21        direct = os::open_direct(path);
22    }
23    ~File() {
24        os::close_fd(direct);
25        os::close_fd(fd);
26        std::filesystem::remove(path);
27    }
28};
29
30enum class Mode { Buffered, Direct, Mapped, Failed };
31
32static bool check(File& file, bool inplace, Mode mode, size_t piece) {
33    ExpertStore::LayerGeo geo;
34    geo.n_exp = 3;
35    const size_t bytes = 65539;
36    for (int r = 0; r < 3; ++r) {
37        geo.file[r] = 0;
38        geo.off[r] = 123 + (size_t) r * 300001;
39        geo.stride[r] = bytes;
40        geo.bytes[r] = bytes;
41        geo.tr[r] = (size_t) r * (((bytes + 255) & ~(size_t) 255) + ExpertStore::kSlack);
42    }
43    geo.blob = geo.tr[2] + bytes;
44    if (mode == Mode::Failed) geo.off[2] = file.bytes.size() + 4096;
45    ExpertStore::Cfg cfg;
46    cfg.budget_bytes = 2 * ((geo.blob + 2 * ExpertStore::kSlack + 4095) & ~(size_t) 4095);
47    cfg.floor_bytes = 0;
48    cfg.threads = 2;
49    cfg.read_in_place = inplace;
50    cfg.piece_bytes = piece;
51    if (mode == Mode::Mapped) cfg.map_chunk = [](void* p, size_t) { return p; };
52    ExpertStore store;
53    if (!store.init(cfg, {geo}, {mode == Mode::Direct ? file.direct : os::kBadFd}, {file.fd})) return false;
54    for (int pass = 0; pass < 3; ++pass) {
55        for (int e = 0; e < 3; ++e) {
56            std::vector<char> ok;
57            const bool ready = store.ensure(0, &e, 1, ok);
58            if (mode == Mode::Failed) { if (ready || ok[0]) return false; continue; }
59            if (!ready || !ok[0]) return false;
60            const int slot = store.find(0, e);
61            if (slot < 0) return false;
62            for (int r = 0; r < 3; ++r) {
63                if (std::memcmp(store.mat(slot, r), file.bytes.data() + geo.off[r] + e * bytes, bytes)) return false;
64                if (store.bytes_ready(slot, r, 0, bytes) != 1 || store.bytes_ready(slot, r, bytes - 1, bytes) != 1) return false;
65                if (mode == Mode::Mapped && store.mat(slot, r) != (const uint8_t*) store.dev_blob(slot) + geo.tr[r]) return false;
66            }
67            store.release(0, &e, 1, ok);
68        }
69    }
70    return true;
71}
72
73// A pinned slot whose read failed must not be handed to another expert while its window still holds it (DS4_LANE_STREAM
74// reads the pieces of it that did land until the window ends).
75static bool pinned_failed_kept(File& file, size_t piece) {
76    ExpertStore::LayerGeo geo;
77    geo.n_exp = 2;
78    const size_t bytes = 65539;
79    for (int r = 0; r < 3; ++r) {
80        geo.file[r] = 0;
81        geo.off[r] = file.bytes.size() + 4096 + (size_t) r * 300001;   // (past the file's end: every read fails)
82        geo.stride[r] = bytes;
83        geo.bytes[r] = bytes;
84        geo.tr[r] = (size_t) r * (((bytes + 255) & ~(size_t) 255) + ExpertStore::kSlack);
85    }
86    geo.blob = geo.tr[2] + bytes;
87    ExpertStore::Cfg cfg;
88    cfg.budget_bytes = (geo.blob + 2 * ExpertStore::kSlack + 4095) & ~(size_t) 4095;   // (one slot)
89    cfg.floor_bytes = 0;
90    cfg.threads = 2;
91    cfg.read_in_place = true;
92    cfg.piece_bytes = piece;
93    ExpertStore store;
94    if (!store.init(cfg, {geo}, {file.direct}, {file.fd})) return false;
95    std::vector<char> oka, okb;
96    std::vector<int> sla, slb;
97    const int a = 0, b = 1;
98    store.begin(0, &a, 1, oka, sla);
99    if (sla[0] < 0) return false;
100    while (!store.loaded(sla[0])) std::this_thread::yield();
101    store.begin(0, &b, 1, okb, slb);
102    if (slb[0] == sla[0]) return false;
103    store.wait_slots(sla, oka, 0, 1);
104    store.wait_slots(slb, okb, 0, 1);
105    store.release(0, &a, 1, oka);
106    store.release(0, &b, 1, okb);
107    return !oka[0];
108}
109
110int main() {
111    File file;
112    if (file.fd == os::kBadFd) return 1;
113    for (size_t piece : {(size_t) 0, (size_t) 4096})
114        if (!pinned_failed_kept(file, piece)) {
115            std::fprintf(stderr, "store layout failed: a pinned failed slot was reused (piece %zu)\n", piece);
116            return 3;
117        }
118    for (bool inplace : {false, true}) {
119        for (Mode mode : {Mode::Buffered, Mode::Direct, Mode::Mapped, Mode::Failed})
120            for (size_t piece : {(size_t) 0, (size_t) 4096, (size_t) 16384}) {   // (pieces: in place only)
121                if (mode == Mode::Direct && file.direct == os::kBadFd) continue;
122                if (!check(file, inplace, mode, piece)) {
123                    std::fprintf(stderr, "store layout failed: inplace %d, mode %d, piece %zu\n", (int) inplace, (int) mode, piece);
124                    return 2;
125                }
126            }
127    }
128    std::puts("store matrix layouts, piece reads, slot reuse, mapped layout and failed reads: OK");
129    return 0;
130}