upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/kernel_writes_test.cpp
diff options
context:
space:
mode:
authorMoriz82 <72836413+Moriz82@users.noreply.github.com>2026-11-25 03:44:31 -0600
committerMoriz82 <72836413+Moriz82@users.noreply.github.com>2026-11-25 03:44:43 -0600
commitba626820ec54776fb0faffa2413727285befe85b (patch)
tree95a99f8029785bec5f3b07b7efc4277e5bc7510b /runtime/test/kernel_writes_test.cpp
parent3de22a19047b168d28a7a11152bf05d18ed0062e (diff)
Integrate post-story-load memory, queue and Vulkan runtime changes
Preserve physical window aliases across remaps, order console list values, and replay bounded indirect graphics with captured draw state. Coordinate pipeline lifetime, texture refresh and queue teardown. Add synthetic CPU and GPU coverage for the integrated paths. Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> Co-authored-by: ivanepsilondelta <264061546+ivanepsilondelta@users.noreply.github.com>
Diffstat (limited to 'runtime/test/kernel_writes_test.cpp')
-rw-r--r--runtime/test/kernel_writes_test.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/runtime/test/kernel_writes_test.cpp b/runtime/test/kernel_writes_test.cpp
index 070ae3488..005e9a934 100644
--- a/runtime/test/kernel_writes_test.cpp
+++ b/runtime/test/kernel_writes_test.cpp
@@ -3,13 +3,20 @@
3// write does not fault such a page in, so the read fails with ERROR_NOACCESS unless the committer runs first. Then 3// write does not fault such a page in, so the read fails with ERROR_NOACCESS unless the committer runs first. Then
4// kernel_writes::hold, the pin a host import keeps over its pages: recorded with no opener set (no tracker yet), seen 4// kernel_writes::hold, the pin a host import keeps over its pages: recorded with no opener set (no tracker yet), seen
5// by for_each_pin until release(), untouched by a file read's pin ending over the same pages. No game data. 5// by for_each_pin until release(), untouched by a file read's pin ending over the same pages. No game data.
6// the pins and the guards of the geometry watch (src/title_geometry_watch.h): a pin made with an OVERLAPPED that does
7// not say pending yet lasts until its call returns; a host thread's write (kernel_writes::write) into a geometry tile
8// the watch made read-only lands and counts as a write; and a tile armed while another guard changes one of its pages
9// gives every page it protected back. No game data.
6#include <windows.h> 10#include <windows.h>
7 11
12#include <atomic>
8#include <cstdint> 13#include <cstdint>
9#include <cstdio> 14#include <cstdio>
10#include <cstring> 15#include <cstring>
16#include <thread>
11#include <vector> 17#include <vector>
12 18
19#include "../src/title_geometry_watch.h"
13#include "../src/title_page_watch.h" 20#include "../src/title_page_watch.h"
14 21
15namespace { 22namespace {
@@ -86,9 +93,119 @@ bool commit_view(const void* address, std::size_t bytes) {
86 return VirtualAlloc(const_cast<std::uint8_t*>(begin), bytes, MEM_COMMIT, PAGE_READWRITE) != nullptr; 93 return VirtualAlloc(const_cast<std::uint8_t*>(begin), bytes, MEM_COMMIT, PAGE_READWRITE) != nullptr;
87} 94}
88 95
96DWORD protection_of(const void* address) {
97 MEMORY_BASIC_INFORMATION info{};
98 return VirtualQuery(address, &info, sizeof(info)) && info.State == MEM_COMMIT ? info.Protect : 0;
99}
100
101// The D3D12 layer's opener as far as the geometry watch goes (open_shadow_pages): its guards over the range lift.
102int g_opened = 0;
103void open_geometry(std::uintptr_t from, std::uintptr_t to) {
104 ++g_opened;
105 leonida::geometry_writes::notify(from, to, leonida::geometry_writes::Event::open);
106}
107
108// A pin made with an OVERLAPPED whose Internal is not STATUS_PENDING yet (ReadFile stores that when it starts) is kept
109// until the call returns; once it returned pending, it lasts until the OVERLAPPED says it completed.
110void check_overlapped_pin() {
111 namespace kw = leonida::kernel_writes;
112 std::vector<std::uint8_t> bytes(0x1000);
113 const auto begin = reinterpret_cast<std::uintptr_t>(bytes.data());
114 const auto pinned = [&] {
115 int n = 0;
116 kw::for_each_pin(begin, begin + bytes.size(), [&](std::uintptr_t, std::uintptr_t) { ++n; });
117 return n;
118 };
119 kw::set_opener([](std::uintptr_t, std::uintptr_t) {});
120 OVERLAPPED at{}; // Internal 0: reads as completed
121 const std::uint64_t pin = kw::begin_write(bytes.data(), bytes.size(), &at);
122 check(pin != 0 && pinned() == 1, "an overlapped pin is kept while its call has not returned (Internal still 0)");
123 at.Internal = static_cast<ULONG_PTR>(STATUS_PENDING);
124 kw::end_write(pin, true);
125 check(pinned() == 1, "returned pending: kept while the OVERLAPPED says pending");
126 at.Internal = 0;
127 check(pinned() == 0 && kw::state().count.load() == 0, "and dropped once it says completed");
128 const std::uint64_t sync = kw::begin_write(bytes.data(), bytes.size(), &at);
129 kw::end_write(sync, false);
130 check(kw::state().count.load() == 0, "an overlapped pin whose call completed ends with the call");
131 kw::set_opener(nullptr);
132}
133
134// A geometry tile over 64 KiB of the title's memory, armed: read-only. A host thread copies into it through
135// kernel_writes::write (as a DirectStorage queue publishes a request): the opener lifts the watch, the copy lands, and
136// the tile's version moved (the geometry copy is refilled at the next submission). The tile is not armed again while
137// a write into it is pinned.
138void check_geometry_write() {
139 namespace gw = leonida::geometry_writes;
140 constexpr std::size_t tile = gw::tile_size;
141 auto* pages = static_cast<std::uint8_t*>(VirtualAlloc(nullptr, tile, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
142 check(pages != nullptr, "a tile of memory");
143 if (!pages) return;
144 const auto va = reinterpret_cast<std::uintptr_t>(pages);
145 gw::Cell* cell = gw::acquire(va);
146 check(gw::arm(va, *cell) && (protection_of(pages + 0x5000) & 0xff) == PAGE_READONLY, "the armed tile is read-only");
147 leonida::kernel_writes::set_opener(&open_geometry);
148 const auto version = cell->version.load();
149 std::vector<std::uint8_t> source(0x3000);
150 for (std::size_t i = 0; i < source.size(); ++i) source[i] = static_cast<std::uint8_t>(i * 13 + 5);
151 bool ok = false;
152 std::thread host([&] { ok = leonida::kernel_writes::write(pages + 0x4800, source.data(), source.size()); });
153 host.join();
154 check(ok && std::memcmp(pages + 0x4800, source.data(), source.size()) == 0,
155 "a host thread's write into the armed tile lands");
156 check(g_opened == 1 && cell->version.load() != version, "the watch was opened first and the tile counts as written");
157 check(gw::arm(va, *cell) && (protection_of(pages) & 0xff) == PAGE_READONLY, "armed again after the write");
158 const std::uint64_t pin = leonida::kernel_writes::begin_write(pages + 0x100, 0x100, nullptr);
159 check(!gw::arm(va, *cell), "not armed while a write into the tile is pinned");
160 leonida::kernel_writes::end_write(pin, false);
161 leonida::kernel_writes::set_opener(nullptr);
162 gw::release(va);
163 check((protection_of(pages) & 0xff) == PAGE_READWRITE && (protection_of(pages + tile - 1) & 0xff) == PAGE_READWRITE,
164 "released: the tile is writable again");
165 VirtualFree(pages, 0, MEM_RELEASE);
166}
167
168// The geometry watch arms a tile while another guard (a slot-133 shadow, under a lock of its own) makes the tile's
169// first page read-only and writable again. VirtualProtect over the tile returns the first page's protection only:
170// taken as every page's, a read-only answer left the others read-only for good once the watch reopened the tile. Every
171// page but the first must be writable after each reopen.
172void check_arm_race() {
173 namespace gw = leonida::geometry_writes;
174 constexpr std::size_t tile = gw::tile_size;
175 auto* pages = static_cast<std::uint8_t*>(VirtualAlloc(nullptr, tile, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
176 if (!pages) return;
177 const auto va = reinterpret_cast<std::uintptr_t>(pages);
178 gw::Cell* cell = gw::acquire(va);
179 std::atomic<bool> stop{false};
180 std::thread other([&] {
181 DWORD old = 0;
182 while (!stop.load(std::memory_order_relaxed)) {
183 VirtualProtect(pages, 0x1000, PAGE_READONLY, &old);
184 VirtualProtect(pages, 0x1000, PAGE_READWRITE, &old);
185 }
186 });
187 int stuck = 0, armed = 0;
188 const auto until = GetTickCount64() + 1500;
189 while (GetTickCount64() < until && !stuck) {
190 armed += gw::arm(va, *cell) ? 1 : 0;
191 gw::notify(va, va + tile, gw::Event::open);
192 for (std::size_t at = 0x1000; at < tile; at += 0x1000)
193 if ((protection_of(pages + at) & 0xff) != PAGE_READWRITE) ++stuck;
194 }
195 stop = true;
196 other.join();
197 std::printf(" %d arms while another guard toggled the first page, %d pages left read-only\n", armed, stuck);
198 check(armed > 0 && stuck == 0, "reopening an armed tile gives back every page it protected");
199 gw::release(va);
200 VirtualFree(pages, 0, MEM_RELEASE);
201}
202
89} // namespace 203} // namespace
90 204
91int main() { 205int main() {
206 check_overlapped_pin();
207 check_geometry_write();
208 check_arm_race();
92 wchar_t dir[MAX_PATH], path[MAX_PATH]; 209 wchar_t dir[MAX_PATH], path[MAX_PATH];
93 GetTempPathW(MAX_PATH, dir); 210 GetTempPathW(MAX_PATH, dir);
94 GetTempFileNameW(dir, L"kwt", 0, path); 211 GetTempFileNameW(dir, L"kwt", 0, path);