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:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 22:03:36 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 22:03:36 -0500
commit9f82f136f68a84bafa745ff61eb6eebb01128654 (patch)
treeb7d6c631ff91421f25e830a93cd37e0187cb0141 /runtime/test/kernel_writes_test.cpp
parent599fe9f2212e1004f93b33a199e19146cf53d7d8 (diff)
d3d12: pin the pages of a live host import
RADV checks the pages of a VK_EXT_external_memory_host import again at every submission, and once one of them is read-only the kernel rejects that submission and every later one. The slot-133 shadow trackers and the geometry watch make copied title pages read-only to see the next write, and a page can be both under a shadow and under a live import. A new leonida::kernel_writes::hold pins a range until release() and opens it. Unlike begin_write it records the pin when no opener is set yet (no tracker exists), so a tracker made later passes over the pages too; ShadowPages and the geometry watch already pass over pinned pages. The slot-133 import (create_host_buffer) and the console descriptor heap import take the pin before vkAllocateMemory, so no guard lands between the writability check and the import, and end it on every path that frees the memory: a declined or failed import and the release. The finding and the design are NateWeav's (coordination issue #14, comments 7141859164 and 7142359977); this implements their description. As in their tree the pin is unconditional; a driver gate is not done. Tests: kernel_writes_test checks hold and release on the pin set (no opener, with an opener, alongside a file read's pin). d3d12_title_va_test in its imports-on mode makes a shadow over pages, checks that its tracker guards them, then imports the same pages and checks that two submissions leave them writable, that the pin ends with the import and that the tracker guards them again afterwards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/kernel_writes_test.cpp')
-rw-r--r--runtime/test/kernel_writes_test.cpp55
1 files changed, 53 insertions, 2 deletions
diff --git a/runtime/test/kernel_writes_test.cpp b/runtime/test/kernel_writes_test.cpp
index 20b347748..070ae3488 100644
--- a/runtime/test/kernel_writes_test.cpp
+++ b/runtime/test/kernel_writes_test.cpp
@@ -1,7 +1,8 @@
1// leonida::kernel_writes::read_file (runtime/src/title_page_watch.h) into memory that is reserved until its first 1// leonida::kernel_writes::read_file (runtime/src/title_page_watch.h) into memory that is reserved until its first
2// touch, as xmem maps the title's physical pages (SEC_RESERVE section views committed by a fault handler): the kernel's 2// touch, as xmem maps the title's physical pages (SEC_RESERVE section views committed by a fault handler): the kernel's
3// write does not fault such a page in, so the read fails with ERROR_NOACCESS unless the committer runs first. No game 3// write does not fault such a page in, so the read fails with ERROR_NOACCESS unless the committer runs first. Then
4// data. 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#include <windows.h> 6#include <windows.h>
6 7
7#include <cstdint> 8#include <cstdint>
@@ -27,6 +28,55 @@ constexpr DWORD kFileBytes = 256 * 1024;
27 28
28std::uint8_t* g_view = nullptr; 29std::uint8_t* g_view = nullptr;
29int g_commits = 0; 30int g_commits = 0;
31int g_opens = 0;
32std::uintptr_t g_opened_begin = 0, g_opened_end = 0;
33
34void count_open(std::uintptr_t begin, std::uintptr_t end) {
35 ++g_opens;
36 g_opened_begin = begin;
37 g_opened_end = end;
38}
39
40// The bytes of [begin, end) the pins cover.
41std::size_t pinned_bytes(std::uintptr_t begin, std::uintptr_t end) {
42 std::size_t bytes = 0;
43 leonida::kernel_writes::for_each_pin(begin, end, [&](std::uintptr_t from, std::uintptr_t to) { bytes += to - from; });
44 return bytes;
45}
46
47void check_holds() {
48 namespace kw = leonida::kernel_writes;
49 static std::uint8_t pages[3 * 4096];
50 const auto begin = reinterpret_cast<std::uintptr_t>(pages);
51 kw::set_opener(nullptr);
52 check(pinned_bytes(begin, begin + sizeof(pages)) == 0, "no pin before the hold");
53 check(kw::hold(pages, 0) == 0 && kw::hold(nullptr, 4096) == 0, "an empty hold pins nothing");
54 // No opener: begin_write records nothing, a hold does (a tracker made later must still pass over the pages).
55 check(kw::begin_write(pages, 4096, nullptr) == 0, "begin_write records nothing without an opener");
56 const std::uint64_t first = kw::hold(pages, 2 * 4096);
57 check(first != 0 && pinned_bytes(begin, begin + sizeof(pages)) == 2 * 4096,
58 "a hold without an opener pins its range");
59 check(pinned_bytes(begin + 2 * 4096, begin + sizeof(pages)) == 0, "and nothing past it");
60 // With an opener: the range is opened once the pin is in.
61 kw::set_opener(&count_open);
62 const std::uint64_t second = kw::hold(pages + 4096, 2 * 4096);
63 check(second != 0 && second != first && g_opens == 1 && g_opened_begin == begin + 4096 &&
64 g_opened_end == begin + 3 * 4096,
65 "a hold with an opener opens exactly its range");
66 // A file read's pin over the same pages ends without ending the holds.
67 const std::uint64_t read = kw::begin_write(pages, sizeof(pages), nullptr);
68 check(read != 0 && pinned_bytes(begin, begin + sizeof(pages)) == 2 * 4096 + 2 * 4096 + sizeof(pages),
69 "a file read's pin adds to the holds");
70 kw::end_write(read, false);
71 check(pinned_bytes(begin, begin + sizeof(pages)) == 4 * 4096, "its end leaves the holds");
72 kw::end_write(first, true); // as a pending overlapped read would: a hold has no OVERLAPPED, so it ends too
73 check(pinned_bytes(begin, begin + 4096) == 0 && pinned_bytes(begin + 4096, begin + sizeof(pages)) == 2 * 4096,
74 "ending one hold leaves the other");
75 kw::release(second);
76 kw::release(0);
77 check(pinned_bytes(begin, begin + sizeof(pages)) == 0, "released, no pin is left");
78 kw::set_opener(nullptr);
79}
30 80
31// Stands in for rt::xmem::commit_physical_range: commits the part of the test view the range covers. 81// Stands in for rt::xmem::commit_physical_range: commits the part of the test view the range covers.
32bool commit_view(const void* address, std::size_t bytes) { 82bool commit_view(const void* address, std::size_t bytes) {
@@ -80,6 +130,7 @@ int main() {
80 "a read into committed memory does not call the committer"); 130 "a read into committed memory does not call the committer");
81 131
82 leonida::kernel_writes::set_committer(nullptr); 132 leonida::kernel_writes::set_committer(nullptr);
133 check_holds();
83 UnmapViewOfFile(g_view); 134 UnmapViewOfFile(g_view);
84 CloseHandle(section); 135 CloseHandle(section);
85 CloseHandle(file); 136 CloseHandle(file);