xmem: wire executable __XMemAllocationHooks__ export into XMemAlloc/X… #546

Open
github-bot wants to merge 1 commit from github-bot/gh-546 into main AGit
Owner

@⁠kotekompotek on GitHub · 2026-11-24 07:28 UTC · original
kotekompotek:work/xmem-allocation-hooks → main · +71 −1 · 3 files · 1 commits · open

Claimed in the coordination issue as: XMEM-HOOKS (#14)

What and why

  • Rebased onto current main.
  • Wired an executable's exported __XMemAllocationHooks__ ({alloc, free}) through pe_loader / shims into XMemAlloc/XMemFree (while XMemAllocDefault/XMemFreeDefault bypass hooks and Loader::~Loader() clears them).
  • Updated runtime/shims/xmem.h comment and docs/xmem_exe_calls.md to accurately reflect that only the symbol export is observed in docs/xmem_exe_calls.md and title runtime hook behaviour is unmeasured.

Tested

  • Built and ran CTest targets pe_loader (Passed, 1.80s) and xmem (Passed, 0.27s) on Windows.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
**@⁠kotekompotek** on GitHub · 2026-11-24 07:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/546) `kotekompotek:work/xmem-allocation-hooks` → `main` · +71 −1 · 3 files · 1 commits · open Claimed in the coordination issue as: XMEM-HOOKS (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why - Rebased onto current `main`. - Wired an executable's exported `__XMemAllocationHooks__` (`{alloc, free}`) through `pe_loader` / `shims` into `XMemAlloc`/`XMemFree` (while `XMemAllocDefault`/`XMemFreeDefault` bypass hooks and `Loader::~Loader()` clears them). - Updated `runtime/shims/xmem.h` comment and `docs/xmem_exe_calls.md` to accurately reflect that only the symbol export is observed in `docs/xmem_exe_calls.md` and title runtime hook behaviour is unmeasured. ### Tested - Built and ran CTest targets `pe_loader` (`Passed`, 1.80s) and `xmem` (`Passed`, 0.27s) on Windows. GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:07:36 +00:00
Author
Owner

@⁠PierreEbele on GitHub · 2026-11-25 00:09 UTC · original

Reviewed 3216db78 by reading the code and running a git merge check. The changed shim, loader and xmem_test targets are Windows/MSVC-only, so I did not compile or run them here.

  1. Conflict and duplicated plumbing. git merge-tree --write-tree origin/main pr546 reports CONFLICT (content) in runtime/src/pe_loader.cpp. Since 522faa2e, main already registers the executable's __XMemAllocationHooks__ at map time (pe_loader.cpp:1505, shims::add_xmem_allocation_hooks) and clears them in ~Loader (:1097). It exposes them as rt::shims::xmem_allocation_hooks() (shims.h, registry.cpp:501-523), which d3d12_xs.cpp:133 already uses. The PR's parallel notify_xmem_allocation_hooks / rt::xmem::XMemAllocationHooks path is therefore redundant. What is still missing on main is the forwarding in xmem.cpp. A rebase that has XMemAlloc/XMemFree read rt::shims::xmem_allocation_hooks() would keep this PR's useful half.
  2. That rebase also removes the ordering gap in this version. Here the pair is installed in Loader::initialise(Module&), after the dependency DLLs' DllMain/TLS attach. An XMemAlloc made in that window comes from the shim heap, but once the hooks are installed its XMemFree would go to the title's free. Main's map-time registration avoids this.
  3. rt::xmem::allocation_hooks() (xmem.h:18) has no caller in the tree.
  4. docs/xmem_exe_calls.md:32 says the loader binds "a non-null {alloc, free} pair". However, xmem.cpp checks alloc and free separately, so a half-null pair would mix allocators. Either require both or reword the doc.

AI-assisted review.

Recommendation: Fix first

**@⁠PierreEbele** on GitHub · 2026-11-25 00:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/546#issuecomment-7148210205) Reviewed 3216db78 by reading the code and running a git merge check. The changed shim, loader and `xmem_test` targets are Windows/MSVC-only, so I did not compile or run them here. 1. **Conflict and duplicated plumbing.** `git merge-tree --write-tree origin/main pr546` reports `CONFLICT (content)` in `runtime/src/pe_loader.cpp`. Since 522faa2e, main already registers the executable's `__XMemAllocationHooks__` at map time (`pe_loader.cpp:1505`, `shims::add_xmem_allocation_hooks`) and clears them in `~Loader` (`:1097`). It exposes them as `rt::shims::xmem_allocation_hooks()` (`shims.h`, `registry.cpp:501-523`), which `d3d12_xs.cpp:133` already uses. The PR's parallel `notify_xmem_allocation_hooks` / `rt::xmem::XMemAllocationHooks` path is therefore redundant. What is still missing on main is the forwarding in `xmem.cpp`. A rebase that has `XMemAlloc`/`XMemFree` read `rt::shims::xmem_allocation_hooks()` would keep this PR's useful half. 2. That rebase also removes the ordering gap in this version. Here the pair is installed in `Loader::initialise(Module&)`, after the dependency DLLs' DllMain/TLS attach. An `XMemAlloc` made in that window comes from the shim heap, but once the hooks are installed its `XMemFree` would go to the title's `free`. Main's map-time registration avoids this. 3. `rt::xmem::allocation_hooks()` (`xmem.h:18`) has no caller in the tree. 4. `docs/xmem_exe_calls.md:32` says the loader binds "a non-null `{alloc, free}` pair". However, `xmem.cpp` checks `alloc` and `free` separately, so a half-null pair would mix allocators. Either require both or reword the doc. AI-assisted review. **Recommendation:** Fix first
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 07:24 UTC · original

Reworked per review: dropped the duplicate hook plumbing and wired XMemAlloc/XMemFree through the existing registry from main (rt::shims::xmem_allocation_hooks()). Forwarding happens only when both callbacks are non-null; XMemAllocDefault/XMemFreeDefault stay on the default allocator path. Rebased on ubi/main (42537e7b6), head is now 9ff7e32a9.

Tested

  • xmem_test (xmem variant) on this branch: all new checks pass - the alloc hook receives size/attributes, the free hook receives pointer/attributes, XMemAllocDefault/XMemFreeDefault bypass the registered hooks, and removing the hooks restores default behavior.
  • Note: xmem_test stops later in the "xmem heap-lock faults" section at its 30 s no-progress watchdog (deadlock between the fault-handler lock and the process heap). Reproduced identically on clean ubi/main without this change, so it is a pre-existing baseline issue, not a regression from this PR. Reported as a finding in the coordination issue.

AI-assisted: yes

**@⁠kotekompotek** on GitHub · 2026-11-25 07:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/546#issuecomment-7155826098) Reworked per review: dropped the duplicate hook plumbing and wired `XMemAlloc`/`XMemFree` through the existing registry from main (`rt::shims::xmem_allocation_hooks()`). Forwarding happens only when both callbacks are non-null; `XMemAllocDefault`/`XMemFreeDefault` stay on the default allocator path. Rebased on `ubi/main` (42537e7b6), head is now `9ff7e32a9`. ### Tested - `xmem_test` (xmem variant) on this branch: all new checks pass - the alloc hook receives size/attributes, the free hook receives pointer/attributes, `XMemAllocDefault`/`XMemFreeDefault` bypass the registered hooks, and removing the hooks restores default behavior. - Note: `xmem_test` stops later in the "xmem heap-lock faults" section at its 30 s no-progress watchdog (deadlock between the fault-handler lock and the process heap). Reproduced identically on clean `ubi/main` without this change, so it is a pre-existing baseline issue, not a regression from this PR. Reported as a finding in the coordination issue. AI-assisted: yes
Author
Owner

@⁠PierreEbele on GitHub · 2026-11-25 07:31 UTC · original

Re-checked 9ff7e32a by reading the code and running a merge check. It is still Windows/MSVC-only, so I did not compile or run it here.

The four points from my earlier review are addressed:

  • The parallel plumbing is gone. git diff --stat origin/main...pr546 now touches only docs/xmem_exe_calls.md, runtime/shims/xmem.cpp and runtime/test/xmem_test.cpp. git merge-tree --write-tree origin/main pr546 reports no conflict.
  • XMemAlloc/XMemFree read main's rt::shims::xmem_allocation_hooks() (xmem.cpp:107-120), so the map-time registration in pe_loader.cpp also removes the ordering gap.
  • The unused rt::xmem::allocation_hooks() is no longer added.
  • Both callbacks are now required in both paths (xmem.cpp:108,116), and the doc matches that.

One small nit: docs/xmem_exe_calls.md:35 says the half-null case is "checked in xmem_test", but the new block only tests a complete pair and its removal. Either add a {&test_alloc_hook, nullptr} case that asserts the hook is not called, or drop "half-null" from the "checked" clause.

AI-assisted review.

Recommendation: Merge (after the doc nit, optional)

**@⁠PierreEbele** on GitHub · 2026-11-25 07:31 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/546#issuecomment-7155950696) Re-checked 9ff7e32a by reading the code and running a merge check. It is still Windows/MSVC-only, so I did not compile or run it here. The four points from my earlier review are addressed: - The parallel plumbing is gone. `git diff --stat origin/main...pr546` now touches only `docs/xmem_exe_calls.md`, `runtime/shims/xmem.cpp` and `runtime/test/xmem_test.cpp`. `git merge-tree --write-tree origin/main pr546` reports no conflict. - `XMemAlloc`/`XMemFree` read main's `rt::shims::xmem_allocation_hooks()` (`xmem.cpp:107-120`), so the map-time registration in `pe_loader.cpp` also removes the ordering gap. - The unused `rt::xmem::allocation_hooks()` is no longer added. - Both callbacks are now required in both paths (`xmem.cpp:108,116`), and the doc matches that. One small nit: `docs/xmem_exe_calls.md:35` says the half-null case is "checked in `xmem_test`", but the new block only tests a complete pair and its removal. Either add a `{&test_alloc_hook, nullptr}` case that asserts the hook is not called, or drop "half-null" from the "checked" clause. AI-assisted review. **Recommendation:** Merge (after the doc nit, optional)
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 07:57 UTC · original

Doc nit addressed in 8c3eb1826: the test now also registers a {alloc, nullptr} pair and asserts neither hook is called (XMemAlloc/XMemFree stay on the default path), matching the "half-null" wording in docs/xmem_exe_calls.md. All hook checks pass in xmem_test (the later "xmem heap-lock faults" watchdog stop reproduces identically on clean ubi/main).

AI-assisted: yes

**@⁠kotekompotek** on GitHub · 2026-11-25 07:57 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/546#issuecomment-7156439580) Doc nit addressed in `8c3eb1826`: the test now also registers a `{alloc, nullptr}` pair and asserts neither hook is called (XMemAlloc/XMemFree stay on the default path), matching the "half-null" wording in `docs/xmem_exe_calls.md`. All hook checks pass in `xmem_test` (the later "xmem heap-lock faults" watchdog stop reproduces identically on clean ubi/main). AI-assisted: yes
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 09:40 UTC · original

@⁠PierreEbele the doc nit is covered at 8c3eb1826: xmem_test now registers {&test_alloc_hook, nullptr} and checks both halves of it — that XMemAlloc does not call the hook and XMemFree stays on the default path ("a half-null hook pair is not called" / "leaves XMemFree on the default path"). All hook checks pass locally; the only red section remains the pre-existing heap-lock deadlock reproduced on clean ubi/main (finding in #1608).

**@⁠kotekompotek** on GitHub · 2026-11-25 09:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/546#issuecomment-7158644428) @⁠PierreEbele the doc nit is covered at `8c3eb1826`: `xmem_test` now registers `{&test_alloc_hook, nullptr}` and checks both halves of it — that `XMemAlloc` does not call the hook and `XMemFree` stays on the default path ("a half-null hook pair is not called" / "leaves XMemFree on the default path"). All hook checks pass locally; the only red section remains the pre-existing heap-lock deadlock reproduced on clean `ubi/main` (finding in #1608).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/546/head:github-bot/gh-546'
git switch 'github-bot/gh-546'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-546'
git switch 'github-bot/gh-546'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-546'
git switch 'github-bot/gh-546'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-546'
git switch 'main'
git merge --squash 'github-bot/gh-546'
git switch 'main'
git merge --ff-only 'github-bot/gh-546'
git switch 'main'
git merge 'github-bot/gh-546'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.