Skip to content

Runtime: record each distinct XMemAlloc/XMemFree attribute word in --trace - #2401

Closed
Sc4ros wants to merge 1 commit into
ItsNotRexed:mainfrom
Sc4ros:xmem-trace-attributes
Closed

Sc4ros wants to merge 1 commit into
ItsNotRexed:mainfrom
Sc4ros:xmem-trace-attributes

Conversation

@Sc4ros

@Sc4ros Sc4ros commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claim: #2300 (comment) (new item: record the raw XMemAlloc/XMemFree attribute words in --trace)

AI-assisted (Claude), tested by running the code below on Windows; see "Tested".

Problem

xmem.cpp ignores the attributes argument of XMemAlloc and XMemFree (every block is 64-byte aligned), and the bit layout of the Scarlett XALLOC_ATTRIBUTES word is not known: the only measured value is 0x5e50000 at one call site. To decode it, someone with the game executable needs the words the game really passes, and logging every allocation would fill any file in seconds.

Change

With gta6rt --trace FILE, the first XMemAlloc and the first XMemFree for each distinct raw attribute word write one xmem_attributes event to the startup trace: api, attributes, size (allocations only) and caller (module+RVA, from the return address). A word already seen for that API writes nothing. With the trace off the cost is one relaxed load per call, the same as the other trace hooks, and allocation behaviour is unchanged. runtime/README.md documents the event in the --trace table.

Test

xmem_test gains test_attribute_trace: it opens a trace file, makes XMemAlloc/XMemFree calls with a repeated word, and checks one event per word and API (two XMemAlloc words, three XMemFree words), the size of the first call, a caller on every event, and no event for a call made before the trace was opened. Without the change to xmem.cpp the new checks fail (6 failures: no events); with it xmem_test passes 28/28.

Tested

Windows 11 (build 26300), MSVC 19.44 (VS 2022 Build Tools, Visual Studio generator, Release), AMD Radeon RX 9060 XT, driver 32.0.32029.2010. Base: 49475796 (main).

  • ctest --test-dir runtime/build -C Release on this branch: 116 run, 110 passed, 6 skipped (they need the package or skip themselves), 0 failed; gameinput is disabled because SDL3 is not installed here.
  • xmem_test before the change to xmem.cpp (new test only): 6 failures; after: OK (28 checks).
  • No game data: the test uses made-up attribute words.

#2199 also edits xmem.cpp (new exports, a different part of the file); whichever lands second may need a trivial rebase.

@LocranKing

Copy link
Copy Markdown
Contributor

Tested 7fda09f4 on Windows 11 Build 26200 (x64), MSVC 14.44.35207, Vulkan SDK 1.4.363.0, RTX 3060 Ti (driver 621.01) with LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=~implicit~: pass

  • scripts\build_runtime.ps1: OK (exit 0, built gta6rt.exe, 0 warnings in xmem.cpp / xmem_test.cpp)
  • runtime\build\xmem_test.exe: xmem_test: OK (28 checks) (exit 0)
  • ctest --test-dir runtime/build -R ^xmem$ --output-on-failure: 1/1 passed (0.02s)

AI-assisted. No game files.

@Dom-in

Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Windows + GPU check: AMD Radeon RX 9070 (RDNA4), Windows 11, MSVC Release, Vulkan SDK 1.4.363. Tested head 7fda09f merged onto main 4947579 (clean merge), full build, full ctest -C Release -j 4: 100% tests passed out of 116 | new failures vs main: none. The main baseline on the same machine passes 107/107; the PR adds its own tests on top.

AI-assisted (Claude Code); runs on my machine.

@Sc4ros

Sc4ros commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main ec8ec4c (pushed 78c02bb). The only conflict was the include block in runtime/shims/xmem.cpp (upstream added <limits>, this PR adds <mutex>, <set>, <utility>); both kept, no other change.

Re-tested after the rebase on Windows 11 build 26300, MSVC 19.44, AMD Radeon RX 9060 XT (driver 32.0.32029.2010): xmem_test.exe xmem_test: OK (28 checks), ctest -R ^xmem$ 1/1 passed.

AI-assisted (Claude Code). No game files.

@FaKL-Code

FaKL-Code commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 7fda09f (merged onto main 4947579) on NVIDIA GeForce RTX 4060 Ti, driver 610.88 (32.0.16.1088), Windows 11 26200, MSVC 14.51.36231 (VS 2026), Vulkan SDK 1.4.363.0: pass. The current head 78c02bb carries the same patch rebased onto ec8ec4c.

  • Clean configure + build of runtime/ (Visual Studio generator, RelWithDebInfo): no new warnings (same C4201/C4456/C4996 set as main).
  • ctest -C RelWithDebInfo -j 4: 116/116 passed, xmem included (loader run on its own; 7 skipped for missing inputs or optional components, gameinput disabled, as on main).
  • command_list, d3d12_com, d3d12_vulkan_device, d3d12_fence_timing, d3d12_descriptor, d3d12_compute and d3d12_state_map also pass with LEONIDA_VK_VALIDATION=1.
  • Heads-up for whoever merges: this PR conflicts with runtime: add XMem virtual memory and write-watch exports #2199 (XMemVirtualAlloc / write watch) in runtime/test/xmem_test.cpp. Checked with git merge-tree on the current heads, 78c02bb and 84f86c5. runtime/shims/xmem.cpp itself merges cleanly. The second of the two to land will need a small rebase of the test file.

AI-assisted (Claude).

@Sc4ros

Sc4ros commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Correction: I have not pushed this rebase yet, so the PR head is still 78c02bb (it conflicts with main in runtime/test/xmem_test.cpp, as FaKL-Code noted). The rebased branch passes xmem_test (44 checks) locally and will be pushed shortly; I will say so here when it is.

@Sc4ros

Sc4ros commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Pushed c92424d: rebased onto main f847748. The conflict with #2199 was in runtime/test/xmem_test.cpp (both PRs add a test function and its call); I kept both, xmem.cpp merged cleanly and nothing else changed. Re-tested on Windows 11 build 26300, MSVC 19.44, AMD Radeon RX 9060 XT (driver 32.0.32029.2010): xmem_test.exe reports xmem_test: OK (44 checks) and ctest -R ^xmem$ passes 1/1.

AI-assisted (Claude Code). No game files.

…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>
@Sc4ros

Sc4ros commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Pushed fdf73f6: rebased onto main 4c52ce2. Main moved again and a new XMem test (test_title_virtual_alloc) landed next to this PR's test_attribute_trace in runtime/test/xmem_test.cpp; I kept both, nothing else changed. xmem_test.exe: xmem_test: OK (83 checks), ctest -R ^xmem$ 1/1 passed (Windows 11 build 26300, MSVC 19.44, RX 9060 XT, driver 32.0.32029.2010).

AI-assisted (Claude Code). No game files.

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested fdf73f6 on Windows 11 (MSVC RelWithDebInfo): full runtime build, then ctest for the shim/loader tests (loader, pe_loader, loader_tls, dll_thread_calls, unimplemented_policy, startup_trace, xmem, process_cmdline, file_system, winhttp_offline, xgameruntime, run_tiny_*, allocator_proxy) — 27 of 27 passed. AI-assisted.

@ddm-j

ddm-j commented Nov 23, 2026

Copy link
Copy Markdown

This PR is already on main: merge 9dc0287f ("Merge pull request #2401", 21:59 UTC) brought in head c92424d3. The rebased head fdf73f66 was pushed two minutes later, so the PR stayed open, and GitHub still shows it as mergeable. Merging it again would break the build: main 0db7fed + fdf73f66 fails to compile with runtime/test/xmem_test.cpp(298,6): error: redefinition of 'test_attribute_trace' (clang-cl + xwin; the duplicate definition would fail with any compiler). Nothing in it is missing from main, as far as the function and its call go, so I think this can be closed. If the rebase changed anything beyond the conflict resolution, a new PR with only that difference would be the way to land it.

AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested fdf73f6 merged with main 98fa503 on Windows (MSVC 14.51): fail. The merge is textually clean, but the result does not compile: main now has its own test_attribute_trace in runtime/test/xmem_test.cpp (line 228, from the earlier xmem fixes) and this PR adds a second function with the same name (line 298 after the merge), so MSVC stops with C2084 'function already has a body'. Building xmem_test on Windows (MSVC 14.51) fails; rename the new test (or fold it into the existing one) when you rebase.

@Sc4ros

Sc4ros commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Closing: the content is already on main. Merge 9dc0287 took head c92424d; my rebase fdf73f6 was pushed two minutes later and only re-adds the same test, which would define test_attribute_trace twice. Checked here: c92424d is an ancestor of main, and nothing in the PR is missing from main. Thanks @ddm-j and @kapitche for catching it.

AI-assisted (Claude Code).

@Sc4ros Sc4ros closed this Nov 23, 2026
ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants