upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/xmem.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/shims/xmem.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/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp35
1 files changed, 33 insertions, 2 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index 7d4a1bcfd..c539a0cb5 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -10,6 +10,7 @@
10// site shows the alignment/type fields, every block is 64-byte aligned (covers cache lines and SIMD) and a size of 10// site shows the alignment/type fields, every block is 64-byte aligned (covers cache lines and SIMD) and a size of
11// zero is rounded up to one byte so the result is a stable, non-null pointer that XMemFree can release. 11// zero is rounded up to one byte so the result is a stable, non-null pointer that XMemFree can release.
12#include <windows.h> 12#include <windows.h>
13#include <intrin.h>
13#include <malloc.h> 14#include <malloc.h>
14#include <psapi.h> 15#include <psapi.h>
15 16
@@ -18,16 +19,46 @@
18#include <cstdio> 19#include <cstdio>
19#include <cstring> 20#include <cstring>
20#include <limits> 21#include <limits>
22#include <mutex>
23#include <set>
24#include <utility>
21 25
22#include "shims.h" 26#include "shims.h"
27#include "startup_trace.h"
23 28
24namespace { 29namespace {
25 30
26constexpr size_t kAlign = 64; 31constexpr size_t kAlign = 64;
27 32
28void* __stdcall XMemAlloc(size_t size, uint64_t /*attributes*/) { return _aligned_malloc(size ? size : 1, kAlign); } 33// gta6rt --trace FILE: the first XMemAlloc and the first XMemFree for each distinct raw attribute word go to the
34// startup trace as an "xmem_attributes" event (api, the word, the size for an allocation, the caller as module+RVA). The
35// bits of the attribute word are not known yet; this records them from a real run without logging every allocation,
36// which would fill the file. Off: one relaxed load per call.
37void note_attributes(bool is_alloc, uint64_t attributes, size_t size, const void* caller) {
38 if (!rt::startup_trace::enabled()) return;
39 static std::mutex mutex;
40 static std::set<std::pair<bool, uint64_t>> seen;
41 {
42 const std::lock_guard lock(mutex);
43 if (!seen.insert({is_alloc, attributes}).second) return;
44 }
45 std::string fields = std::string("\"api\":\"") + (is_alloc ? "XMemAlloc" : "XMemFree") +
46 "\",\"attributes\":" + rt::startup_trace::quote(rt::startup_trace::hex(attributes));
47 if (is_alloc) fields += ",\"size\":" + std::to_string(size);
48 fields += ",\"caller\":" +
49 rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller)));
50 rt::startup_trace::event("xmem_attributes", fields);
51}
29 52
30void __stdcall XMemFree(void* p, uint64_t /*attributes*/) { _aligned_free(p); } 53void* __stdcall XMemAlloc(size_t size, uint64_t attributes) {
54 note_attributes(true, attributes, size, _ReturnAddress());
55 return _aligned_malloc(size ? size : 1, kAlign);
56}
57
58void __stdcall XMemFree(void* p, uint64_t attributes) {
59 note_attributes(false, attributes, 0, _ReturnAddress());
60 _aligned_free(p);
61}
31 62
32// Measured call sites pass a zeroed 64-byte structure. Only the MEMORY_BASIC_INFORMATION-compatible prefix is 63// Measured call sites pass a zeroed 64-byte structure. Only the MEMORY_BASIC_INFORMATION-compatible prefix is
33// known: callers read at least the base and size fields. Fill that prefix from VirtualQueryEx and zero the rest. 64// known: callers read at least the base and size fields. Fill that prefix from VirtualQueryEx and zero the rest.