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:
authorquackottu <224479491+quackottu@users.noreply.github.com>2026-11-23 21:05:03 +0530
committerGitHub <noreply@github.com>2026-11-23 09:35:03 -0600
commit695792de7a93e054fb52f540ccf4cf308d122f18 (patch)
tree12861aa410f8ea4b0b76930ee40987bf09bb5be6 /runtime/test/xmem_test.cpp
parent44c413e6455a66a93ba73b53861fa554199d3067 (diff)
xmem: record XMemVirtualAlloc's attribute word and drop the PAGE_GRAPHICS_* protection bits (retail exe GPU pools) (#6908)
Squash-merged after diff review of PR #6908 - xmem: record XMemVirtualAlloc's attribute word and drop the PAGE_GRAPHICS_* protection bits (retail exe GPU pools)
Diffstat (limited to 'runtime/test/xmem_test.cpp')
-rw-r--r--runtime/test/xmem_test.cpp50
1 files changed, 42 insertions, 8 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp
index aa9ba1d31..edc5fd33c 100644
--- a/runtime/test/xmem_test.cpp
+++ b/runtime/test/xmem_test.cpp
@@ -224,14 +224,35 @@ void test_virtual_alloc(XMemVirtualAllocFn virtual_alloc, XMemGetWriteWatchFn ge
224 check(large_page == nullptr && GetLastError() == ERROR_NOT_SUPPORTED, 224 check(large_page == nullptr && GetLastError() == ERROR_NOT_SUPPORTED,
225 "unsupported large-page allocation is rejected before host allocation"); 225 "unsupported large-page allocation is rejected before host allocation");
226 226
227 SetLastError(ERROR_SUCCESS); 227 // The retail executable's GPU pools: the attribute word is recorded, and the PAGE_GRAPHICS_* bits are dropped.
228 void* const attributed = virtual_alloc(nullptr, page_size, MEM_RESERVE | MEM_COMMIT, 1, PAGE_READWRITE); 228 MEMORY_BASIC_INFORMATION mbi{};
229 check(attributed == nullptr && GetLastError() == ERROR_NOT_SUPPORTED, 229 void* const pool = virtual_alloc(nullptr, SIZE_T{8} << 30, MEM_RESERVE | 0x20400000, 0x21, PAGE_READWRITE);
230 "nonzero XMem allocation attributes are rejected before host allocation"); 230 check(pool != nullptr, "attributes 0x21 with 64 KiB pages reserves 8 GiB");
231 if (pool) {
232 check(VirtualQuery(pool, &mbi, sizeof mbi) && mbi.State == MEM_RESERVE, "the 8 GiB pool is reserve-only");
233 check(VirtualFree(pool, 0, MEM_RELEASE), "release the 8 GiB pool");
234 }
235
236 void* const noaccess = virtual_alloc(nullptr, 0x10000, MEM_RESERVE | 0x20400000, 0x3, 0x801);
237 check(noaccess != nullptr, "attributes 0x3 with protect PAGE_GRAPHICS_NOACCESS|PAGE_NOACCESS succeeds");
238 if (noaccess) {
239 check(VirtualQuery(noaccess, &mbi, sizeof mbi) && mbi.AllocationProtect == PAGE_NOACCESS,
240 "protect 0x801 becomes PAGE_NOACCESS");
241 check(VirtualFree(noaccess, 0, MEM_RELEASE), "release the PAGE_NOACCESS pool");
242 }
243
244 void* const gpu = virtual_alloc(nullptr, 0x10000, MEM_RESERVE | MEM_COMMIT, 0x183, 0x22004);
245 check(gpu != nullptr, "attributes 0x183 with protect 0x22004 succeeds");
246 if (gpu) {
247 check(VirtualQuery(gpu, &mbi, sizeof mbi) && mbi.Protect == PAGE_READWRITE, "protect 0x22004 becomes PAGE_READWRITE");
248 static_cast<volatile uint8_t*>(gpu)[0] = 0x5a;
249 check(static_cast<volatile uint8_t*>(gpu)[0] == 0x5a, "the 0x183 allocation is writable");
250 check(VirtualFree(gpu, 0, MEM_RELEASE), "release the 0x183 allocation");
251 }
231} 252}
232 253
233// gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off. 254// gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off.
234void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem) { 255void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem, XMemVirtualAllocFn virtual_alloc) {
235 wchar_t temp[MAX_PATH]; 256 wchar_t temp[MAX_PATH];
236 GetTempPathW(MAX_PATH, temp); 257 GetTempPathW(MAX_PATH, temp);
237 const std::wstring path = 258 const std::wstring path =
@@ -250,6 +271,9 @@ void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem) {
250 free_mem(b, 0x5e50000); // same word again: no second event 271 free_mem(b, 0x5e50000); // same word again: no second event
251 free_mem(c, 0x99); 272 free_mem(c, 0x99);
252 free_mem(nullptr, 0x77); // a free-only word is recorded too 273 free_mem(nullptr, 0x77); // a free-only word is recorded too
274 void* const v = virtual_alloc ? virtual_alloc(nullptr, 0x10000, MEM_RESERVE | MEM_COMMIT, 0x183, PAGE_READWRITE)
275 : nullptr;
276 if (v) VirtualFree(v, 0, MEM_RELEASE);
253 rt::startup_trace::close(); 277 rt::startup_trace::close();
254 278
255 std::ifstream in(path); 279 std::ifstream in(path);
@@ -261,14 +285,16 @@ void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem) {
261 for (size_t at = log.find(needle); at != std::string::npos; at = log.find(needle, at + 1)) ++n; 285 for (size_t at = log.find(needle); at != std::string::npos; at = log.find(needle, at + 1)) ++n;
262 return n; 286 return n;
263 }; 287 };
264 check(count("\"kind\":\"xmem_attributes\"") == 5, "five events: two XMemAlloc words and three XMemFree words"); 288 check(count("\"kind\":\"xmem_attributes\"") == 6, "six events: two XMemAlloc words, three XMemFree words and one XMemVirtualAlloc word");
289 check(count("\"api\":\"XMemVirtualAlloc\",\"attributes\":\"0x183\",\"size\":65536") == 1,
290 "XMemVirtualAlloc 0x183 is recorded once, with its size");
265 check(count("\"api\":\"XMemAlloc\",\"attributes\":\"0x5e50000\",\"size\":4096") == 1, 291 check(count("\"api\":\"XMemAlloc\",\"attributes\":\"0x5e50000\",\"size\":4096") == 1,
266 "XMemAlloc 0x5e50000 is recorded once, with the size of the first call"); 292 "XMemAlloc 0x5e50000 is recorded once, with the size of the first call");
267 check(count("\"api\":\"XMemAlloc\",\"attributes\":\"0x99\",\"size\":16") == 1, "XMemAlloc 0x99 is recorded once"); 293 check(count("\"api\":\"XMemAlloc\",\"attributes\":\"0x99\",\"size\":16") == 1, "XMemAlloc 0x99 is recorded once");
268 check(count("\"api\":\"XMemFree\",\"attributes\":\"0x5e50000\"") == 1, "XMemFree 0x5e50000 is recorded once"); 294 check(count("\"api\":\"XMemFree\",\"attributes\":\"0x5e50000\"") == 1, "XMemFree 0x5e50000 is recorded once");
269 check(count("\"api\":\"XMemFree\",\"attributes\":\"0x77\"") == 1, "XMemFree 0x77 is recorded"); 295 check(count("\"api\":\"XMemFree\",\"attributes\":\"0x77\"") == 1, "XMemFree 0x77 is recorded");
270 check(count("\"attributes\":\"0x1111\"") == 0, "nothing was recorded while the trace was off"); 296 check(count("\"attributes\":\"0x1111\"") == 0, "nothing was recorded while the trace was off");
271 check(count("\"caller\":\"") == 5, "every event names its caller"); 297 check(count("\"caller\":\"") == 6, "every event names its caller");
272 DeleteFileW(path.c_str()); 298 DeleteFileW(path.c_str());
273} 299}
274 300
@@ -369,6 +395,14 @@ void test_title_virtual_alloc() {
369 check(alloc != nullptr, what); 395 check(alloc != nullptr, what);
370 if (!alloc) continue; 396 if (!alloc) continue;
371 397
398 void* const gfx = alloc(nullptr, 0x10000, MEM_RESERVE | MEM_COMMIT, 0x2004); // PAGE_GRAPHICS_READWRITE
399 check(gfx != nullptr, "title VirtualAlloc with protect 0x2004 succeeds");
400 if (gfx) {
401 MEMORY_BASIC_INFORMATION mbi{};
402 check(VirtualQuery(gfx, &mbi, sizeof mbi) && mbi.Protect == PAGE_READWRITE,
403 "protect 0x2004 commits PAGE_READWRITE");
404 VirtualFree(gfx, 0, MEM_RELEASE);
405 }
372 for (const DWORD bits : {kMemGraphics, kMemTitle, kMemGraphics | kMemTitle, DWORD{0}}) { 406 for (const DWORD bits : {kMemGraphics, kMemTitle, kMemGraphics | kMemTitle, DWORD{0}}) {
373 void* const p = alloc(nullptr, 0x10000, MEM_RESERVE | MEM_COMMIT | bits, PAGE_READWRITE); 407 void* const p = alloc(nullptr, 0x10000, MEM_RESERVE | MEM_COMMIT | bits, PAGE_READWRITE);
374 std::snprintf(what, sizeof what, "%s!VirtualAlloc with type bits %#lx returns memory", dll, 408 std::snprintf(what, sizeof what, "%s!VirtualAlloc with type bits %#lx returns memory", dll,
@@ -430,7 +464,7 @@ int main() {
430 if (virtual_alloc && get_write_watch && reset_write_watch) { 464 if (virtual_alloc && get_write_watch && reset_write_watch) {
431 test_virtual_alloc(virtual_alloc, get_write_watch, reset_write_watch); 465 test_virtual_alloc(virtual_alloc, get_write_watch, reset_write_watch);
432 } 466 }
433 test_attribute_trace(alloc, free_mem); 467 test_attribute_trace(alloc, free_mem, virtual_alloc);
434 if (transfer) { 468 if (transfer) {
435 test_transfer_memory(transfer); 469 test_transfer_memory(transfer);
436 test_transfer_trace(transfer); 470 test_transfer_trace(transfer);