upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/xmem_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/xmem_test.cpp')
-rw-r--r--runtime/test/xmem_test.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp
index 4087df4d7..7e4948f1f 100644
--- a/runtime/test/xmem_test.cpp
+++ b/runtime/test/xmem_test.cpp
@@ -20,6 +20,9 @@ using XMemFreeFn = void(__stdcall*)(void*, uint64_t);
20using XMemVirtualQueryFn = SIZE_T(__stdcall*)(HANDLE, const void*, void*); 20using XMemVirtualQueryFn = SIZE_T(__stdcall*)(HANDLE, const void*, void*);
21// BOOL XMemGetWorkingSetStatistics(UINT32 kind, stats*): the FSR DLL passes kind 0x20000001 at every call site. 21// BOOL XMemGetWorkingSetStatistics(UINT32 kind, stats*): the FSR DLL passes kind 0x20000001 at every call site.
22using XMemGetWorkingSetStatisticsFn = BOOL(__stdcall*)(uint32_t, void*); 22using XMemGetWorkingSetStatisticsFn = BOOL(__stdcall*)(uint32_t, void*);
23using XMemVirtualAllocFn = void*(WINAPI*)(void*, SIZE_T, DWORD, ULONGLONG, DWORD);
24using XMemGetWriteWatchFn = UINT(WINAPI*)(DWORD, void*, SIZE_T, void**, ULONG_PTR*, ULONG*);
25using XMemResetWriteWatchFn = UINT(WINAPI*)(void*, SIZE_T);
23constexpr uint32_t kMeasuredKind = 0x20000001; 26constexpr uint32_t kMeasuredKind = 0x20000001;
24 27
25int g_checks = 0; 28int g_checks = 0;
@@ -129,6 +132,78 @@ void test_virtual_query(XMemVirtualQueryFn query) {
129 check(bad == 0, "XMemVirtualQuery on an invalid address returns 0 without crashing"); 132 check(bad == 0, "XMemVirtualQuery on an invalid address returns 0 without crashing");
130} 133}
131 134
135void test_virtual_alloc(XMemVirtualAllocFn virtual_alloc, XMemGetWriteWatchFn get_write_watch,
136 XMemResetWriteWatchFn reset_write_watch) {
137 SYSTEM_INFO system_info{};
138 GetSystemInfo(&system_info);
139 const SIZE_T page_size = system_info.dwPageSize;
140 check(page_size != 0, "GetSystemInfo reports a non-zero page size");
141 if (page_size == 0) return;
142
143 const SIZE_T region_size = page_size * 3;
144 void* const base = virtual_alloc(nullptr, region_size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, 0,
145 PAGE_READWRITE);
146 check(base != nullptr, "XMemVirtualAlloc creates a synthetic write-watch region");
147 if (!base) return;
148
149 void* watched[4]{};
150 ULONG_PTR count = 4;
151 ULONG granularity = 0;
152 UINT status = get_write_watch(0, base, region_size, watched, &count, &granularity);
153 check(status == NO_ERROR && count == 0, "new write-watch region starts with no dirty pages");
154 check(status == NO_ERROR && granularity == page_size, "write-watch reports the host page granularity");
155
156 auto* const bytes = static_cast<volatile uint8_t*>(base);
157 bytes[0] = 0xa1;
158 bytes[page_size * 2] = 0xb2;
159
160 count = 4;
161 status = get_write_watch(0, base, region_size, watched, &count, &granularity);
162 bool saw_first_page = false;
163 bool saw_third_page = false;
164 bool all_addresses_are_page_aligned = count <= 4;
165 for (ULONG_PTR i = 0; i < count && i < 4; ++i) {
166 const uintptr_t address = reinterpret_cast<uintptr_t>(watched[i]);
167 const uintptr_t start = reinterpret_cast<uintptr_t>(base);
168 all_addresses_are_page_aligned &= address >= start && (address - start) % page_size == 0;
169 saw_first_page |= address == start;
170 saw_third_page |= address == start + page_size * 2;
171 }
172 check(status == NO_ERROR && count == 2 && saw_first_page && saw_third_page,
173 "write-watch reports each written page by its relative address");
174 check(all_addresses_are_page_aligned, "write-watch addresses are page aligned and within the region");
175
176 count = 4;
177 status = get_write_watch(0, base, region_size, watched, &count, &granularity);
178 check(status == NO_ERROR && count == 2, "query-only write-watch leaves dirty pages recorded");
179
180 status = reset_write_watch(base, region_size);
181 check(status == NO_ERROR, "XMemResetWriteWatch clears the synthetic region");
182 count = 4;
183 status = get_write_watch(0, base, region_size, watched, &count, &granularity);
184 check(status == NO_ERROR && count == 0, "reset write-watch query returns no dirty pages");
185
186 bytes[page_size] = 0xc3;
187 count = 4;
188 status = get_write_watch(0, base, region_size, watched, &count, &granularity);
189 check(status == NO_ERROR && count == 1 &&
190 reinterpret_cast<uintptr_t>(watched[0]) == reinterpret_cast<uintptr_t>(base) + page_size,
191 "write-watch records writes again after reset");
192
193 check(VirtualFree(base, 0, MEM_RELEASE), "release the synthetic write-watch region");
194
195 SetLastError(ERROR_SUCCESS);
196 void* const large_page = virtual_alloc(nullptr, page_size, MEM_RESERVE | MEM_COMMIT | 0x20000000, 0,
197 PAGE_READWRITE);
198 check(large_page == nullptr && GetLastError() == ERROR_NOT_SUPPORTED,
199 "unsupported large-page allocation is rejected before host allocation");
200
201 SetLastError(ERROR_SUCCESS);
202 void* const attributed = virtual_alloc(nullptr, page_size, MEM_RESERVE | MEM_COMMIT, 1, PAGE_READWRITE);
203 check(attributed == nullptr && GetLastError() == ERROR_NOT_SUPPORTED,
204 "nonzero XMem allocation attributes are rejected before host allocation");
205}
206
132void print_result() { 207void print_result() {
133 if (g_failures == 0) { 208 if (g_failures == 0) {
134 std::printf("xmem_test: OK (%d checks)\n", g_checks); 209 std::printf("xmem_test: OK (%d checks)\n", g_checks);
@@ -148,11 +223,20 @@ int main() {
148 const auto query = reinterpret_cast<XMemVirtualQueryFn>(rt::shims::find("xmem.dll", "XMemVirtualQuery")); 223 const auto query = reinterpret_cast<XMemVirtualQueryFn>(rt::shims::find("xmem.dll", "XMemVirtualQuery"));
149 const auto get_stats = 224 const auto get_stats =
150 reinterpret_cast<XMemGetWorkingSetStatisticsFn>(rt::shims::find("xmem.dll", "XMemGetWorkingSetStatistics")); 225 reinterpret_cast<XMemGetWorkingSetStatisticsFn>(rt::shims::find("xmem.dll", "XMemGetWorkingSetStatistics"));
226 const auto virtual_alloc =
227 reinterpret_cast<XMemVirtualAllocFn>(rt::shims::find("xmem.dll", "XMemVirtualAlloc"));
228 const auto get_write_watch =
229 reinterpret_cast<XMemGetWriteWatchFn>(rt::shims::find("xmem.dll", "XMemGetWriteWatch"));
230 const auto reset_write_watch =
231 reinterpret_cast<XMemResetWriteWatchFn>(rt::shims::find("xmem.dll", "XMemResetWriteWatch"));
151 232
152 check(alloc != nullptr, "registry exposes xmem.dll!XMemAlloc"); 233 check(alloc != nullptr, "registry exposes xmem.dll!XMemAlloc");
153 check(free_mem != nullptr, "registry exposes xmem.dll!XMemFree"); 234 check(free_mem != nullptr, "registry exposes xmem.dll!XMemFree");
154 check(query != nullptr, "registry exposes xmem.dll!XMemVirtualQuery"); 235 check(query != nullptr, "registry exposes xmem.dll!XMemVirtualQuery");
155 check(get_stats != nullptr, "registry exposes xmem.dll!XMemGetWorkingSetStatistics"); 236 check(get_stats != nullptr, "registry exposes xmem.dll!XMemGetWorkingSetStatistics");
237 check(virtual_alloc != nullptr, "registry exposes xmem.dll!XMemVirtualAlloc");
238 check(get_write_watch != nullptr, "registry exposes xmem.dll!XMemGetWriteWatch");
239 check(reset_write_watch != nullptr, "registry exposes xmem.dll!XMemResetWriteWatch");
156 240
157 if (!alloc || !free_mem || !query || !get_stats) { 241 if (!alloc || !free_mem || !query || !get_stats) {
158 print_result(); 242 print_result();
@@ -162,6 +246,9 @@ int main() {
162 test_alloc(alloc, free_mem); 246 test_alloc(alloc, free_mem);
163 test_working_set(get_stats); 247 test_working_set(get_stats);
164 test_virtual_query(query); 248 test_virtual_query(query);
249 if (virtual_alloc && get_write_watch && reset_write_watch) {
250 test_virtual_alloc(virtual_alloc, get_write_watch, reset_write_watch);
251 }
165 252
166 print_result(); 253 print_result();
167 return g_failures ? 1 : 0; 254 return g_failures ? 1 : 0;