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:
authorSc4ros <353102343+Sc4ros@users.noreply.github.com>2026-11-22 17:55:56 +0100
committerSc4ros <353102343+Sc4ros@users.noreply.github.com>2026-11-22 21:18:43 +0100
commitc92424d3f5cc377bfe2f2e05f2005b98e2c2306e (patch)
tree98fcaa3fbf572b03fe82536750d60d5a265761a7 /runtime/test/xmem_test.cpp
parentf847748cc76bd3777f9c070c4034eb2fc1c04e64 (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.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp
index 7e4948f1f..127f11564 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
16namespace { 20namespace {
17 21
@@ -204,6 +208,48 @@ void test_virtual_alloc(XMemVirtualAllocFn virtual_alloc, XMemGetWriteWatchFn ge
204 "nonzero XMem allocation attributes are rejected before host allocation"); 208 "nonzero XMem allocation attributes are rejected before host allocation");
205} 209}
206 210
211// gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off.
212void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem) {
213 wchar_t temp[MAX_PATH];
214 GetTempPathW(MAX_PATH, temp);
215 const std::wstring path =
216 std::wstring(temp) + L"xmem_trace_test_" + std::to_wstring(GetCurrentProcessId()) + L".jsonl";
217 DeleteFileW(path.c_str());
218
219 void* untraced = alloc(64, 0x1111); // trace not open yet: nothing to write, nothing to crash
220 check(untraced != nullptr, "XMemAlloc with the trace off still allocates");
221 free_mem(untraced, 0x1111);
222
223 check(rt::startup_trace::open(path.c_str()), "open the startup trace file");
224 void* a = alloc(4096, 0x5e50000);
225 void* b = alloc(8192, 0x5e50000); // same word again: no second event
226 void* c = alloc(16, 0x99);
227 free_mem(a, 0x5e50000);
228 free_mem(b, 0x5e50000); // same word again: no second event
229 free_mem(c, 0x99);
230 free_mem(nullptr, 0x77); // a free-only word is recorded too
231 rt::startup_trace::close();
232
233 std::ifstream in(path);
234 std::stringstream text;
235 text << in.rdbuf();
236 const std::string log = text.str();
237 auto count = [&](const std::string& needle) {
238 int n = 0;
239 for (size_t at = log.find(needle); at != std::string::npos; at = log.find(needle, at + 1)) ++n;
240 return n;
241 };
242 check(count("\"kind\":\"xmem_attributes\"") == 5, "five events: two XMemAlloc words and three XMemFree words");
243 check(count("\"api\":\"XMemAlloc\",\"attributes\":\"0x5e50000\",\"size\":4096") == 1,
244 "XMemAlloc 0x5e50000 is recorded once, with the size of the first call");
245 check(count("\"api\":\"XMemAlloc\",\"attributes\":\"0x99\",\"size\":16") == 1, "XMemAlloc 0x99 is recorded once");
246 check(count("\"api\":\"XMemFree\",\"attributes\":\"0x5e50000\"") == 1, "XMemFree 0x5e50000 is recorded once");
247 check(count("\"api\":\"XMemFree\",\"attributes\":\"0x77\"") == 1, "XMemFree 0x77 is recorded");
248 check(count("\"attributes\":\"0x1111\"") == 0, "nothing was recorded while the trace was off");
249 check(count("\"caller\":\"") == 5, "every event names its caller");
250 DeleteFileW(path.c_str());
251}
252
207void print_result() { 253void print_result() {
208 if (g_failures == 0) { 254 if (g_failures == 0) {
209 std::printf("xmem_test: OK (%d checks)\n", g_checks); 255 std::printf("xmem_test: OK (%d checks)\n", g_checks);
@@ -249,6 +295,7 @@ int main() {
249 if (virtual_alloc && get_write_watch && reset_write_watch) { 295 if (virtual_alloc && get_write_watch && reset_write_watch) {
250 test_virtual_alloc(virtual_alloc, get_write_watch, reset_write_watch); 296 test_virtual_alloc(virtual_alloc, get_write_watch, reset_write_watch);
251 } 297 }
298 test_attribute_trace(alloc, free_mem);
252 299
253 print_result(); 300 print_result();
254 return g_failures ? 1 : 0; 301 return g_failures ? 1 : 0;