diff options
| author | Sc4ros <353102343+Sc4ros@users.noreply.github.com> | 2026-11-22 17:55:56 +0100 |
|---|---|---|
| committer | Sc4ros <353102343+Sc4ros@users.noreply.github.com> | 2026-11-22 23:01:17 +0100 |
| commit | fdf73f6696a7bab6c99e9650046fa110d0e73885 (patch) | |
| tree | d1c4b3d996f317406c9b7241b207d0d9666b26a3 /runtime/test/xmem_test.cpp | |
| parent | 4c52ce27a6cf4af2bbf07a343ede39c5f1572b8d (diff) | |
Runtime: record each distinct XMemAlloc/XMemFree attribute word in --trace
xmem.cpp ignores the attributes argument, and the bits of the Scarlett
XALLOC_ATTRIBUTES word are not known yet. With gta6rt --trace FILE, the first
XMemAlloc and the first XMemFree for each distinct raw attribute word now
write an "xmem_attributes" event (api, the word, the size for an allocation,
the caller as module+RVA), so the bits can be decoded from a real run without
logging every allocation. Off: one relaxed load per call.
xmem_test opens a trace and checks one event per word and API, the size of the
first call, a caller on every event, and nothing recorded while the trace is
off. README.md documents the event.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/xmem_test.cpp')
| -rw-r--r-- | runtime/test/xmem_test.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 1ad4080f7..f40171753 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp | |||
| @@ -10,8 +10,12 @@ | |||
| 10 | #include <cstdint> | 10 | #include <cstdint> |
| 11 | #include <cstdio> | 11 | #include <cstdio> |
| 12 | #include <cstring> | 12 | #include <cstring> |
| 13 | #include <fstream> | ||
| 14 | #include <sstream> | ||
| 15 | #include <string> | ||
| 13 | 16 | ||
| 14 | #include "shims.h" | 17 | #include "shims.h" |
| 18 | #include "startup_trace.h" | ||
| 15 | 19 | ||
| 16 | namespace { | 20 | namespace { |
| 17 | 21 | ||
| @@ -232,6 +236,48 @@ void test_title_virtual_alloc() { | |||
| 232 | } | 236 | } |
| 233 | } | 237 | } |
| 234 | 238 | ||
| 239 | // gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off. | ||
| 240 | void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem) { | ||
| 241 | wchar_t temp[MAX_PATH]; | ||
| 242 | GetTempPathW(MAX_PATH, temp); | ||
| 243 | const std::wstring path = | ||
| 244 | std::wstring(temp) + L"xmem_trace_test_" + std::to_wstring(GetCurrentProcessId()) + L".jsonl"; | ||
| 245 | DeleteFileW(path.c_str()); | ||
| 246 | |||
| 247 | void* untraced = alloc(64, 0x1111); // trace not open yet: nothing to write, nothing to crash | ||
| 248 | check(untraced != nullptr, "XMemAlloc with the trace off still allocates"); | ||
| 249 | free_mem(untraced, 0x1111); | ||
| 250 | |||
| 251 | check(rt::startup_trace::open(path.c_str()), "open the startup trace file"); | ||
| 252 | void* a = alloc(4096, 0x5e50000); | ||
| 253 | void* b = alloc(8192, 0x5e50000); // same word again: no second event | ||
| 254 | void* c = alloc(16, 0x99); | ||
| 255 | free_mem(a, 0x5e50000); | ||
| 256 | free_mem(b, 0x5e50000); // same word again: no second event | ||
| 257 | free_mem(c, 0x99); | ||
| 258 | free_mem(nullptr, 0x77); // a free-only word is recorded too | ||
| 259 | rt::startup_trace::close(); | ||
| 260 | |||
| 261 | std::ifstream in(path); | ||
| 262 | std::stringstream text; | ||
| 263 | text << in.rdbuf(); | ||
| 264 | const std::string log = text.str(); | ||
| 265 | auto count = [&](const std::string& needle) { | ||
| 266 | int n = 0; | ||
| 267 | for (size_t at = log.find(needle); at != std::string::npos; at = log.find(needle, at + 1)) ++n; | ||
| 268 | return n; | ||
| 269 | }; | ||
| 270 | check(count("\"kind\":\"xmem_attributes\"") == 5, "five events: two XMemAlloc words and three XMemFree words"); | ||
| 271 | check(count("\"api\":\"XMemAlloc\",\"attributes\":\"0x5e50000\",\"size\":4096") == 1, | ||
| 272 | "XMemAlloc 0x5e50000 is recorded once, with the size of the first call"); | ||
| 273 | check(count("\"api\":\"XMemAlloc\",\"attributes\":\"0x99\",\"size\":16") == 1, "XMemAlloc 0x99 is recorded once"); | ||
| 274 | check(count("\"api\":\"XMemFree\",\"attributes\":\"0x5e50000\"") == 1, "XMemFree 0x5e50000 is recorded once"); | ||
| 275 | check(count("\"api\":\"XMemFree\",\"attributes\":\"0x77\"") == 1, "XMemFree 0x77 is recorded"); | ||
| 276 | check(count("\"attributes\":\"0x1111\"") == 0, "nothing was recorded while the trace was off"); | ||
| 277 | check(count("\"caller\":\"") == 5, "every event names its caller"); | ||
| 278 | DeleteFileW(path.c_str()); | ||
| 279 | } | ||
| 280 | |||
| 235 | void print_result() { | 281 | void print_result() { |
| 236 | if (g_failures == 0) { | 282 | if (g_failures == 0) { |
| 237 | std::printf("xmem_test: OK (%d checks)\n", g_checks); | 283 | std::printf("xmem_test: OK (%d checks)\n", g_checks); |
| @@ -277,6 +323,7 @@ int main() { | |||
| 277 | if (virtual_alloc && get_write_watch && reset_write_watch) { | 323 | if (virtual_alloc && get_write_watch && reset_write_watch) { |
| 278 | test_virtual_alloc(virtual_alloc, get_write_watch, reset_write_watch); | 324 | test_virtual_alloc(virtual_alloc, get_write_watch, reset_write_watch); |
| 279 | } | 325 | } |
| 326 | test_attribute_trace(alloc, free_mem); | ||
| 280 | 327 | ||
| 281 | test_title_virtual_alloc(); | 328 | test_title_virtual_alloc(); |
| 282 | 329 | ||