okami.mom/strata

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

Niko1221Merge f-029-par-amd (4772f0cd9)16e08b50c

main
5.6 KiB107 linesraw
1// Windows selftest for the named-section backing of PinnedArena (the replicas' shared expert arena).
2// Parent: two arenas on one section name (same bytes, header refused for another pack / a bigger size, the load state word),
3// then a CHILD PROCESS that opens the same section, reads what the parent wrote and writes back; the parent closes its first
4// arena while the second still holds the section (the pages must stay), and a third arena joins the live section.  After the
5// last close, a new arena with the same name starts empty (the section is gone with its last handle).
6#ifdef _WIN32
7#include "strata/core/pinned.hpp"
8
9#include <windows.h>
10
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <string>
15#include <vector>
16
17namespace {
18constexpr uint64_t kBytes = 64u << 10;
19constexpr uint64_t kPackHash = 0x0123456789abcdefull;
20}  // namespace
21
22int main(int argc, char** argv) {
23    const std::vector<uint64_t> no_bounds;
24    if (argc >= 3 && std::strcmp(argv[1], "child") == 0) {
25        const std::string name = argv[2];
26        strata::core::PinnedArena a(kBytes, no_bounds, 0, name, kPackHash);
27        if (!a.valid()) { std::fprintf(stderr, "child: cannot open: %s\n", a.note.c_str()); return 2; }
28        if (a.data()[123] != 0x5a) { std::fprintf(stderr, "child: did not see the parent's byte\n"); return 3; }
29        if (a.shared_state == nullptr || a.shared_state->load() != 4242u) { std::fprintf(stderr, "child: state word is not the parent's\n"); return 4; }
30        a.data()[456] = 0xa5;
31        a.shared_state->store(~uint64_t{0});     // (filled)
32        return 0;
33    }
34
35    int fail = 0;
36    const std::string name = "win:Local\\strata-pinned-test-" + std::to_string((unsigned long long) GetCurrentProcessId());
37    {
38        auto* first = new strata::core::PinnedArena(kBytes, no_bounds, 0, name, kPackHash);
39        if (!first->valid()) {
40            std::fprintf(stderr, "first section failed: %s\n", first->note.c_str());
41            return 1;
42        }
43        first->data()[123] = 0x5a;
44        if (first->shared_state == nullptr) { std::fprintf(stderr, "no state word\n"); fail = 1; }
45        else if (first->shared_state->load() != 0) { std::fprintf(stderr, "a new section's state word is not 0\n"); fail = 1; }
46        else first->shared_state->store(4242u);
47
48        auto* second = new strata::core::PinnedArena(kBytes, no_bounds, 0, name, kPackHash);
49        if (!second->valid()) { std::fprintf(stderr, "second section failed: %s\n", second->note.c_str()); fail = 1; }
50        else {
51            if (second->data()[123] != 0x5a) { std::fprintf(stderr, "same name did not expose the same bytes\n"); fail = 1; }
52            if (second->shared_state == nullptr || second->shared_state->load() != 4242u) { std::fprintf(stderr, "state word not shared\n"); fail = 1; }
53        }
54        {
55            strata::core::PinnedArena wrong_pack(kBytes, no_bounds, 0, name, kPackHash + 1);
56            if (wrong_pack.valid() || wrong_pack.note.find("pack hash") == std::string::npos) {
57                std::fprintf(stderr, "pack mismatch was not refused: %s\n", wrong_pack.note.c_str());
58                fail = 1;
59            }
60            strata::core::PinnedArena wrong_size(kBytes * 2, no_bounds, 0, name, kPackHash);
61            if (wrong_size.valid()) { std::fprintf(stderr, "a bigger size was not refused\n"); fail = 1; }
62        }
63
64        // a child process opens the live section
65        char exe[MAX_PATH];
66        GetModuleFileNameA(nullptr, exe, MAX_PATH);
67        std::string cmd = std::string("\"") + exe + "\" child \"" + name + "\"";
68        STARTUPINFOA si{};
69        si.cb = sizeof si;
70        PROCESS_INFORMATION pi{};
71        std::vector<char> cl(cmd.begin(), cmd.end());
72        cl.push_back('\0');
73        if (!CreateProcessA(nullptr, cl.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi)) {
74            std::fprintf(stderr, "cannot start the child (error %lu)\n", GetLastError());
75            fail = 1;
76        } else {
77            WaitForSingleObject(pi.hProcess, 120000);
78            DWORD code = 99;
79            GetExitCodeProcess(pi.hProcess, &code);
80            CloseHandle(pi.hProcess);
81            CloseHandle(pi.hThread);
82            if (code != 0) { std::fprintf(stderr, "the child failed with %lu\n", code); fail = 1; }
83            if (first->data()[456] != 0xa5) { std::fprintf(stderr, "the child's byte did not arrive\n"); fail = 1; }
84            if (first->shared_state->load() != ~uint64_t{0}) { std::fprintf(stderr, "the child's state word did not arrive\n"); fail = 1; }
85        }
86
87        delete first;                           // the creator is gone; the second still holds the section
88        if (second != nullptr && second->valid()) {
89            if (second->data()[123] != 0x5a || second->data()[456] != 0xa5) { std::fprintf(stderr, "bytes lost when the first arena closed\n"); fail = 1; }
90            strata::core::PinnedArena third(kBytes, no_bounds, 0, name, kPackHash);
91            if (!third.valid() || third.data()[123] != 0x5a) { std::fprintf(stderr, "a late arena did not join the live section\n"); fail = 1; }
92        }
93        delete second;
94    }
95    {
96        strata::core::PinnedArena again(kBytes, no_bounds, 0, name, kPackHash);   // (every handle was closed: a new, empty section)
97        if (!again.valid() || again.data()[123] != 0 || again.shared_state->load() != 0) {
98            std::fprintf(stderr, "the section survived its last handle\n");
99            fail = 1;
100        }
101    }
102    std::printf("pinned_shared_win_test: %s\n", fail ? "FAILED" : "OK");
103    return fail;
104}
105#else
106int main() { return 77; }   // (skipped: the Linux build has pinned_shared_test)
107#endif