runtime: xcrdapi stand-in COM objects are never freed at final Release #2788

Open
opened 2026-11-27 01:22:47 +00:00 by github-bot · 2 comments
Owner

@⁠triggered02 on GitHub · 2026-11-26 09:18 UTC · original

Commit or build

Upstream main at 63451c5e.

Summary

The DirectStorage stand-in (src/startup_stubs/xcrdapi.cpp) never frees its COM objects. make(unsigned kind) (line 295) heap-allocates an Object / FileObject / QueueObject for every OpenFile, CreateQueue, and CreateStatusArray, but release(Object*) (line 282) only decrements the refcount and, at zero, closes the queue's error event — it never deletes the object. The factory is a process singleton, so every file/queue/status object created during a run leaks for the life of the process.

Evidence (code read on main)

// xcrdapi.cpp:282
unsigned long __stdcall release(Object* self)
{
    const auto refs = static_cast<unsigned long>(InterlockedDecrement(&self->refs));
    if (refs == 0 && self->kind == queue) {
        auto& object = *static_cast<QueueObject*>(self);
        const std::lock_guard guard(object.error_mutex);
        if (object.error_event) CloseHandle(std::exchange(object.error_event, nullptr));
    }
    return refs;   // no delete
}

make() (line 295) news QueueObject / FileObject / Object; nothing ever deletes them.

Why a naive delete-at-zero is not the fix

  • queue_worker (line 953) runs for (;;), holds the raw self pointer with no AddRef, and never exits; its thread handle is CloseHandled right after ResumeThread. Deleting the queue at refcount zero would leave the worker running on freed memory.
  • Queue entries store raw Object* array (status), void* fence, and read the request's source FileObject* at run time, none AddRefed. A guest Release() of a file or status array while an entry is pending would dangle.

So a correct fix needs a worker shutdown/join protocol plus reference accounting for entry-held objects (and the factory singleton must stay alive), not just delete this at zero.

Synthetic repro (proposed, source-built, no game data)

A fixture that drives the stand-in factory directly:

  1. Create a status array and a file, release each to zero, assert the object is freed (a live-object counter export, or a heap snapshot before/after).
  2. Create a queue, enqueue a request referencing a file plus a status entry, release the file and status array while the entries are pending, submit, wait the fence, then release the queue; assert no use-after-free and that all objects are freed.
  3. Fail-first mutant: delete at refcount zero without the worker shutdown / entry accounting -> the queue case must crash or fail.

Acceptance: a new startup_xcrdapi_lifetime CTest (no GPU adapter required).

Not established

  • No fixture has been run yet; this is a code-evident lifetime gap.
  • The per-run object count is small (about 60 OpenFile + 34 queue/status creations in a 300 s diagnostic run), so this is a correctness leak, not a multi-GiB commit-pressure source. It is filed separately from the texture/heap commit work (#2379, #1199).

AI-assisted; code read on main, synthetic repro proposed.

**@⁠triggered02** on GitHub · 2026-11-26 09:18 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/2788) ### Commit or build Upstream `main` at `63451c5e`. ### Summary The DirectStorage stand-in (`src/startup_stubs/xcrdapi.cpp`) never frees its COM objects. `make(unsigned kind)` (line 295) heap-allocates an `Object` / `FileObject` / `QueueObject` for every `OpenFile`, `CreateQueue`, and `CreateStatusArray`, but `release(Object*)` (line 282) only decrements the refcount and, at zero, closes the queue's error event — it never `delete`s the object. The factory is a process singleton, so every file/queue/status object created during a run leaks for the life of the process. ### Evidence (code read on `main`) ```cpp // xcrdapi.cpp:282 unsigned long __stdcall release(Object* self) { const auto refs = static_cast<unsigned long>(InterlockedDecrement(&self->refs)); if (refs == 0 && self->kind == queue) { auto& object = *static_cast<QueueObject*>(self); const std::lock_guard guard(object.error_mutex); if (object.error_event) CloseHandle(std::exchange(object.error_event, nullptr)); } return refs; // no delete } ``` `make()` (line 295) `new`s `QueueObject` / `FileObject` / `Object`; nothing ever deletes them. ### Why a naive delete-at-zero is not the fix - `queue_worker` (line 953) runs `for (;;)`, holds the raw `self` pointer with no `AddRef`, and never exits; its thread handle is `CloseHandle`d right after `ResumeThread`. Deleting the queue at refcount zero would leave the worker running on freed memory. - Queue entries store raw `Object* array` (status), `void* fence`, and read the request's source `FileObject*` at run time, none `AddRef`ed. A guest `Release()` of a file or status array while an entry is pending would dangle. So a correct fix needs a worker shutdown/join protocol plus reference accounting for entry-held objects (and the factory singleton must stay alive), not just `delete this` at zero. ### Synthetic repro (proposed, source-built, no game data) A fixture that drives the stand-in factory directly: 1. Create a status array and a file, release each to zero, assert the object is freed (a live-object counter export, or a heap snapshot before/after). 2. Create a queue, enqueue a request referencing a file plus a status entry, release the file and status array while the entries are pending, submit, wait the fence, then release the queue; assert no use-after-free and that all objects are freed. 3. Fail-first mutant: `delete` at refcount zero without the worker shutdown / entry accounting -> the queue case must crash or fail. Acceptance: a new `startup_xcrdapi_lifetime` CTest (no GPU adapter required). ### Not established - No fixture has been run yet; this is a code-evident lifetime gap. - The per-run object count is small (about 60 `OpenFile` + 34 queue/status creations in a 300 s diagnostic run), so this is a correctness leak, not a multi-GiB commit-pressure source. It is filed separately from the texture/heap commit work (#2379, #1199). AI-assisted; code read on `main`, synthetic repro proposed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 01:22:47 +00:00
Author
Owner

@⁠RenzoWit35 on GitHub · 2026-11-26 10:20 UTC · original

I can take this. Plan: pending entries hold a reference on their status array and source file; at the last release a queue is marked closing and its worker finishes the submitted entries and then frees the queue and its work (a queue without a worker is freed directly), so nothing blocks inside Release. Test startup_xcrdapi_lifetime: a test-side fence whose Signal blocks holds the worker while the file, status array and queue are released; a live-object count must stay non-zero until the gate opens and then reach zero. Fail-first control: delete at refcount zero without the hand-off must fail the count check. Source-built, no GPU, no title data.

AI-assisted.

**@⁠RenzoWit35** on GitHub · 2026-11-26 10:20 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/2788#issuecomment-7189224313) I can take this. Plan: pending entries hold a reference on their status array and source file; at the last release a queue is marked closing and its worker finishes the submitted entries and then frees the queue and its work (a queue without a worker is freed directly), so nothing blocks inside Release. Test `startup_xcrdapi_lifetime`: a test-side fence whose Signal blocks holds the worker while the file, status array and queue are released; a live-object count must stay non-zero until the gate opens and then reach zero. Fail-first control: delete at refcount zero without the hand-off must fail the count check. Source-built, no GPU, no title data. AI-assisted.
Author
Owner

@⁠Not-Saii on GitHub · 2026-11-26 11:49 UTC · original

Verified on main at 25eb746e (Windows x64, dev preset, no GPU needed): ctest --preset dev --no-tests=error -R 'xcrdapi' --output-on-failure passes 2/2. startup_xcrdapi_lifetime passes, and startup_xcrdapi_lifetime_naive_control (WILL_FAIL) catches the delete-at-zero mutant. Fixed by #2800; this can be closed.

**@⁠Not-Saii** on GitHub · 2026-11-26 11:49 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/2788#issuecomment-7191206905) Verified on `main` at `25eb746e` (Windows x64, `dev` preset, no GPU needed): `ctest --preset dev --no-tests=error -R 'xcrdapi' --output-on-failure` passes 2/2. `startup_xcrdapi_lifetime` passes, and `startup_xcrdapi_lifetime_naive_control` (`WILL_FAIL`) catches the delete-at-zero mutant. Fixed by #2800; this can be closed.
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/R0Y00--Xbox2Windows#2788
No description provided.