Skip to content

runtime/shims: implement XCRDAPI.dll!DStorageGetFactory stub factory - #7784

Closed
emirtugra-dag wants to merge 3 commits into
ProjectLeonida:mainfrom
emirtugra-dag:fix/retail-shim-1123-2013
Closed

emirtugra-dag wants to merge 3 commits into
ProjectLeonida:mainfrom
emirtugra-dag:fix/retail-shim-1123-2013

Conversation

@emirtugra-dag

Copy link
Copy Markdown

Claimed in the coordination issue as: unclaimed (Stop 5 in docs/retail_boot_chain.md, XCRDAPI.dll!DStorageGetFactory)

What and why

Implements XCRDAPI.dll!DStorageGetFactory (Stop 5 in docs/retail_boot_chain.md) so the retail executable does not fail with a null factory pointer at startup:

  • runtime/shims/xcrdapi.cpp: Implements DStorageGetFactory(const IID*, void**) returning S_OK with a process-lifetime StubDStorageFactory COM stub (64-slot vtable with atomic QueryInterface/AddRef/Release refcounting and one-time logged E_NOTIMPL on slots 3..63).
  • runtime/shims/registry.cpp: Classifies xcrdapi.dll!DStorageGetFactory as ReturnClass::HResult.
  • runtime/CMakeLists.txt & scripts/build_runtime.ps1: Wires shims/xcrdapi.cpp into gta6rt, test targets, CTest (xcrdapi), and the cl.exe fallback build.
  • runtime/test/xcrdapi_test.cpp & runtime/test/unimplemented_policy_test.cpp: Adds C++ unit tests covering owns_dll, library_loader resolution, argument validation, IUnknown refcounting, and vtable slots 3..63.
  • tests/test_xcrdapi_shim.py & docs/: Adds structural/registration tests and updates docs/retail_boot_chain.md and docs/runtime_imports.md.

Tested

  • Environment: Windows 11, MSVC (x64), Python 3.12 (CPU/runtime verification only; no GPU pipeline run on the PR targets).
  • xcrdapi_test.exe: 22/22 checks passed.
  • ctest (13 runtime/shim suites including xcrdapi, unimplemented_policy, pe_loader): 13/13 passed.
  • Python unit & doc/CI registration tests (test_xcrdapi_shim.py, test_ci_registration.py, test_ctest_registration.py, test_exe_checklist.py) and scripts/check_no_game_data.py: all passed.

AI-assisted: yes

Checklist

  • 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.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted review of d3d95fbbe on Windows 11. QueryInterface reports that the factory supports IID_NULL. A caller that checks for an optional interface therefore receives a false success.

In runtime/test/xcrdapi_test.cpp, insert this before void* qi_out:

    constexpr IID unsupported{}; // IID_NULL
    void* result = nullptr;
    const HRESULT hr = qi(factory, &unsupported, &result);
    std::printf("IID_NULL: hr=%08lx same_factory=%d\n", static_cast<unsigned long>(hr), result == factory);
    check(hr == E_NOINTERFACE && result == nullptr, "unsupported IID is refused");
    if (SUCCEEDED(hr) && result == factory) release(factory);

Build with cmake --build <build> --config RelWithDebInfo --target xcrdapi_test. Run <build>\RelWithDebInfo\xcrdapi_test.exe. I get IID_NULL: hr=00000000 same_factory=1 and 23 check(s), 1 failure(s). The expected result for an unsupported IID is E_NOINTERFACE and a null output pointer. Please check the IID before adding a reference or assigning the factory pointer.

The original xcrdapi CTest passes. All three tests in tests/test_xcrdapi_shim.py pass.

@emirtugra-dag

Copy link
Copy Markdown
Author

Thanks for catching this and testing on Windows 11! Updated DStorageGetFactory and factory_query_interface to validate the requested IID against IID_IUnknown, IDStorageFactory, and the factory IID, returning E_NOINTERFACE with a null out-pointer (and without incrementing the refcount) for IID_NULL and other unsupported interfaces.

Re-verified locally with xcrdapi_test.exe (IID_NULL: hr=80004002 same_factory=0, 27/27 checks passed) and tests/test_xcrdapi_shim.py.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Re-tested 1c226ad13 on Windows 11. The original IID_NULL check now returns E_NOINTERFACE with a null pointer. All 27 unmodified C++ checks and all three Python tests pass.

A non-null unsupported IID still changes the interfaces available on an existing factory. The test first confirms that QueryInterface(kUnknownInterface) returns E_NOINTERFACE. Calling DStorageGetFactory with that same IID then returns S_OK. The same live factory now returns S_OK for the interface it just rejected.

Insert this before void* qi_out in runtime/test/xcrdapi_test.cpp:

    void* acquired = nullptr;
    void* queried = nullptr;
    const HRESULT get_hr = get_factory(&kUnknownInterface, &acquired);
    const HRESULT query_hr = qi(factory, &kUnknownInterface, &queried);
    std::printf("unknown IID: get=%08lx query=%08lx same=%d\n",
                static_cast<unsigned long>(get_hr), static_cast<unsigned long>(query_hr),
                acquired == factory && queried == factory);
    check(get_hr == E_NOINTERFACE && acquired == nullptr, "factory rejects unsupported IID");
    check(query_hr == E_NOINTERFACE && queried == nullptr, "interface set stays fixed");
    if (SUCCEEDED(query_hr)) release(queried);
    if (SUCCEEDED(get_hr)) release(acquired);

Build xcrdapi_test in RelWithDebInfo and run it. Actual output: unknown IID: get=00000000 query=00000000 same=1. The two added checks fail. Please validate the requested IID against a fixed set of supported interfaces before publishing the factory. A request for an unsupported IID must leave the existing factory’s interface set unchanged.

AI-assisted.

@emirtugra-dag

Copy link
Copy Markdown
Author

Fixed in 2e537bc.

StubDStorageFactory no longer carries a mutable factory_iid. Both dstorage_get_factory and factory_query_interface now validate *riid against the fixed supports_iid(const IID&) set (IID_IUnknown and IDStorageFactory {6924ea0c-c3cd-4826-b10a-f64f4ed927c1}) before incrementing the reference count or writing *ppv, so requesting an unsupported IID from DStorageGetFactory returns E_NOINTERFACE with *ppv = nullptr and leaves the existing factory's interface set unchanged. Added the regression check to runtime/test/xcrdapi_test.cpp and tests/test_xcrdapi_shim.py.

Re-tested on Windows 11 (cmake --build runtime/build --target xcrdapi_test gta6rt):

  • .\runtime\build\xcrdapi_test.exe: IID_NULL: hr=80004002 same_factory=0, unknown IID: get=80004002 query=80004002 same=0, 29 check(s), 0 failure(s)
  • ctest --test-dir runtime/build -R "^xcrdapi$" --output-on-failure: 1/1 passed
  • python -I tests/test_xcrdapi_shim.py: 3/3 passed

AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted recheck on Windows 11. Commit 2e537bcba resolves my finding. Both factory acquisition and the subsequent query reject the same unsupported IID with E_NOINTERFACE and a null output pointer. All 29 checks in xcrdapi_test.exe and all three tests in tests/test_xcrdapi_shim.py pass. I also built the same C++ test against 1c226ad13. Both calls then returned S_OK and the same factory pointer, and exactly two checks failed. This verifies the interface behavior with synthetic inputs.

@CowBoYdu76

Copy link
Copy Markdown
Contributor

Tested 2e537bc merged onto main 867d922 (clean merge) on Windows 11 build 26300, MSVC 19.44, Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1, with two GPUs picked by LEONIDA_VK_DEVICE: RTX 4070 SUPER (NVIDIA 616.64) and Intel UHD 770 (101.7092): pass.

RTX 4070 SUPER UHD 770
ctest -R "xcrdapi|unimplemented_policy|loader|startup" (xcrdapi, unimplemented_policy, loader, pe_loader, pe_loader_bounds, loader_tls, startup_trace, run_startup_cmdline) 8/8 8/8
full ctest 274/276, the same 2 failures as main 867d922 (d3d12_vulkan_device, d3d12_depth_resolve) not run

tests/test_xcrdapi_shim.py, test_ci_registration.py and test_ctest_registration.py pass too.

Note for the coordinator: #8461 (KevinPratap, a console DirectStorage with real file-to-memory reads) and #7175 also handle XCRDAPI!DStorageGetFactory, so these overlap. This stub moves the boot past the null-factory check of Stop 5, but every factory method after that returns E_NOTIMPL, so the title will most likely stop at its first OpenFile/CreateQueue. It would help to decide whether this lands first as a step, or whether #8461 supersedes it.

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

@Kerwinss

Copy link
Copy Markdown

Tested 2e537bc on Windows 11 Pro 10.0.26300, MSVC 14.51 (VS Build Tools 2026), Windows SDK 10.0.26100, NVIDIA GeForce RTX 4060 / 621.24, Khronos Vulkan headers via -DLEONIDA_VULKAN_HEADERS_FETCH=ON. On its own: pass, no regression (Merged onto main 867d922 (clean), full runtime build, ctest -C RelWithDebInfo -j4: 238/240, the two failures are main's own on this GPU (d3d12_depth_resolve, #7985; d3d12_vulkan_device:6574); xcrdapi passes).

But it overlaps #4348, which is stop 5's fix in docs/retail_boot_chain.md and is part of the boot ladder (ASTROWwwW:ladder). runtime/shims/dstorage.cpp in #4348 already registers {"xcrdapi.dll", "DStorageGetFactory", ...} with the real factory, and this PR registers the same name with a stub whose slots 3..63 return E_NOTIMPL.

  • rt::shims::Table::Table (runtime/shims/registry.cpp) does table()[dll!name] = fn, so the last static table to initialise silently wins. Across translation units that order is unspecified (link order). With both merged, the title could get the stub instead of runtime: console DirectStorage (DStorageGetFactory) with file-to-memory reads #4348's factory, with no error, and stop at the first OpenFile / CreateQueue.
  • Merging this PR onto ASTROWwwW:ladder (7a325be) conflicts in runtime/CMakeLists.txt, runtime/shims/registry.cpp, runtime/test/unimplemented_policy_test.cpp and scripts/build_runtime.ps1.

So I'd keep #4348 as stop 5's fix and not land this one alongside it, or at least drop the xcrdapi.dll entry here. A duplicate-name check in Table::Table would also catch the next such overlap.

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

Cryptoteep added a commit to Cryptoteep/Leonida that referenced this pull request Nov 23, 2026
…gainst main

Update docs/retail_boot_chain.md "What clears it" column against current main:
- Stops 1, 2, 6, 10, 11 landed on main (ItsNotRexed#6919, 042c5f3: physical pages,
  2 MiB pages, and placeholder reservations).
- Stop 3 landed on main (e417570: XErrorReport in runtime/shims/xgameruntime.cpp).
- Replace closed unmerged PR ItsNotRexed#4313 (Stop 4) with tracking open PRs ItsNotRexed#8379, ItsNotRexed#7524.
- Replace closed unmerged PR ItsNotRexed#4348 (Stop 5) with active takeover PR ItsNotRexed#8461, ItsNotRexed#7784.
- Link open tracking PRs for Stops 7, 9, 12 (ItsNotRexed#8851, ItsNotRexed#9061, ItsNotRexed#2490, ItsNotRexed#9045, ItsNotRexed#8414).

Addresses agulerman's finding in ItsNotRexed#8868.
@rideinstead

Copy link
Copy Markdown

✅ Automated security review: CLEAN (commit 2e537bcb)

  • No findings.

Scope: static analysis of added diff lines only; no code was executed. Please review findings manually.

@kristofbarany

Copy link
Copy Markdown

Retail boot with this PR: tested 2e537bc merged onto main 6312c05 (clean merge), gta6rt built on NVIDIA GeForce RTX 4070, driver 621.24, native Windows 11 Pro 26300, MSVC 14.44.35207, Ninja RelWithDebInfo, gta6rt --run on my own copy of the 1.0.2.1 executable (docs/boot.md runbook). The stub does not get past boot chain stop 5.

No game data used. AI-assisted (Claude Code).

@emirtugra-dag

emirtugra-dag commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

Closing this stub PR as superseded by #8461 (@KevinPratap's full console DirectStorage factory takeover of #4348).

Keeping #7784 open would duplicate/collide on the xcrdapi.dll!DStorageGetFactory registration in runtime/shims/registry.cpp (as noted in checks on both PRs), whereas #8461 already implements the factory, queues, file reads, and status arrays and gets retail boot past Stop 5 (verified by @kristofbarany on #8461).

Further DirectStorage testing, reviews, and follow-up stages should continue on #8461.

Thanks to @shdann, @CowBoYdu76, @Kerwinss, @rideinstead, and @kristofbarany for testing and reviewing.

AI-assisted.

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.

6 participants