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

Open
opened 2026-11-26 09:18:30 +00:00 by triggered02 · 2 comments
triggered02 commented 2026-11-26 09:18:30 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by triggered02 on 2026-11-26T09:18:30Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6623424735 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/2788). Originally posted by [triggered02](https://github.com/triggered02) on 2026-11-26T09:18:30Z. --- ### 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.
RenzoWit35 commented 2026-11-26 11:02:59 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-26T10:20:00Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7189224313 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/2788#issuecomment-7189224313). Originally posted by [RenzoWit35](https://github.com/RenzoWit35) on 2026-11-26T10:20:00Z. --- 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.
Not-Saii commented 2026-11-26 12:04:57 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Not-Saii on 2026-11-26T11:49:13Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7191206905 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/2788#issuecomment-7191206905). Originally posted by [Not-Saii](https://github.com/Not-Saii) on 2026-11-26T11:49:13Z. --- 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.
Sign in to join this conversation.
No labels
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.

Reference
R0Y00/Xbox2Windows#2780
No description provided.