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:
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);