Skip to content

xgameruntime: serve XUserImpl (offline local user) through QueryApiImpl - #3363

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
romainveurrier:rt/xuserimpl
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
romainveurrier:rt/xuserimpl

Conversation

@romainveurrier

Copy link
Copy Markdown
Contributor

Item: new RT item, claimed in #3232 (claim).

Why

A GDK title links XUserAddAsync, XUserGetId, XUserGetGamertag and the rest statically (xgameruntime.lib). Each thunk asks QueryApiImpl for XUserImpl and calls one slot with the function's own arguments after this. That mechanism is measured on the package's own DLLs (docs/runtime_imports.md, "Run-time loaded modules"). QueryApiImpl refused XUserImpl, so XUserAddAsync, one of the first calls a title makes, could only fail.

What

runtime/shims/xgameruntime_user.cpp, in its own file, so the other class PRs (#2203, #2218) only meet it at one line of QueryApiImpl.

  • Sources: class and interface ids and the slot order come from xgameruntime-docs (facts only, as for main's other classes). Signatures, enum values and E_GAMEUSER_* codes come from Microsoft's public GDK reference (the XUser pages and the error-code table). No GDK header or code is used.
  • Interfaces: the class id doubles as IXUserImpl's; IXUserImpl2–6 answer with the same 50-slot vtable; IXUserGamertagImpl (XUserGetGamertag) is a second interface of the object.
  • One offline local user, always signed in:
    • XUserAddAsync / XUserAddResult, silent or not. AllowGuests together with AddDefaultUserSilently is E_INVALIDARG, as documented.
    • XUserAddByIdWithUiAsync: the local id adds the user; any other id is E_GAMEUSER_USER_NOT_FOUND.
    • Fixed non-zero user and local ids, with XUserFindUserById / ByLocalId finding them and anything else being E_GAMEUSER_USER_NOT_FOUND.
    • Not a guest, SignedIn, Adult, and every privilege (XUserCheckPrivilege); XUserResolvePrivilegeWithUiAsync completes with S_OK; XUserIsStoreUser is true; XUserGetMaxUsers is 1.
    • Gamertag from LEONIDA_GAMERTAG (15 printable ASCII characters at most), Player otherwise. Classic, Modern and UniqueModern give the same text, ModernSuffix is empty, and gamertagUsed counts the terminator.
    • Handles: one allocation each, closed once by XUserCloseHandle; XUserCompare is 0 for any two, since they all name the one user.
    • Change events: only tracked as tokens (unregister is true once), because the user never changes.
  • Not available offline: token, gamer-picture, MSA-token and resolve-issue requests fail synchronously before starting, so no XAsyncBlock is left waiting. XUserGetSignOutDeferral is E_GAMEUSER_DEFERRAL_NOT_AVAILABLE.
  • Logged once on use: the docs' unknown slot 15, the PC-only IXUserImpl5 slots, and IXUserImpl6's undocumented sign-out trio.

Not measured yet: no package DLL calls an XUser* slot (libHttpClient.GDK.dll only holds the GUID table, up to IXUserImpl5), so the slot order rests on xgameruntime-docs until the executable's first boot. The order matched on every slot measured for XThreadingImpl, XErrorImpl and XNetworkingImpl. docs/runtime_imports.md says so.

Tests

xgameruntime_user_test (40 checks) gets the class through rt::shims::find("xgameruntime.dll", "QueryApiImpl"), as a thunk would, and calls each served slot with the documented arguments. It covers:

  • the interface chain and the gamertag face;
  • add (silent, picker, by id), find, compare, duplicate and close, with the open-handle count back to 0 at the end;
  • state, age group, privileges, change-event tokens, the deferral error, the token failure;
  • every gamertag component, a buffer one byte short, and an unknown component.

A second ctest run sets LEONIDA_GAMERTAG=Tester42 and checks it; a gamertag that is too long falls back to Player with a message.

Tested

Fedora 44, clang-cl 22.1.8 with xwin (SDK 10.0.26100), /W4: no warnings in the new files.

  • Under Wine 11.21: xgameruntime, xgameruntime_user and xgameruntime_user_gamertag pass.
  • Full Wine suite: 127/131. The 4 failures are the same on main (fsr_dispatch and bvh_builder are the clang 22.1.8 crash; device_proxy and d3d12_present compare against Wine's D3D12).
  • scripts/build_runtime.ps1's source list includes the new file. Not run: MSVC, native Windows.

XGameSaveImpl is the natural next class; it's waiting for @IAmNotSiob's answer on #279.

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

🤖 Generated with Claude Code

A GDK title links the XUser* functions statically (xgameruntime.lib); each
thunk asks QueryApiImpl for XUserImpl and calls one slot with the function's
arguments after `this` (docs/runtime_imports.md). QueryApiImpl refused the
class, so XUserAddAsync, the call a title makes at boot before nearly
anything else, could not succeed.

runtime/shims/xgameruntime_user.cpp serves it, in its own file so the other
class PRs touching xgameruntime.cpp only meet it at one line:

- ids and slot order from xgameruntime-docs (facts only), signatures, enum
  values and E_GAMEUSER_* codes from the public GDK reference;
- IXUserImpl..IXUserImpl6 answer with one 50-slot vtable, IXUserGamertagImpl
  (XUserGetGamertag) with a second;
- one offline user, always signed in: XUserAddAsync (silent or not;
  AllowGuests with AddDefaultUserSilently is E_INVALIDARG), fixed ids,
  FindUserById/ByLocalId, Duplicate/Close/Compare, not a guest, adult, every
  privilege, ResolvePrivilegeWithUi, AddByIdWithUi, IsStoreUser, change-event
  tokens; gamertag from LEONIDA_GAMERTAG, "Player" otherwise;
- tokens, gamer picture, MSA tokens and resolve-issue fail synchronously
  before starting; no sign-out deferral; private and PC-only slots log once.

xgameruntime_user_test (40 checks) drives every served slot; a second ctest
run checks LEONIDA_GAMERTAG.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CharlyRien

Copy link
Copy Markdown

Code review of 056661d (AI-assisted), with a synthetic probe executable in a private copy, built with MSVC / Ninja on Windows 11 23H2. Nothing was pushed. xgameruntime, xgameruntime_user and xgameruntime_user_gamertag pass. No problem found on the boot path: the user is signed in, an adult and not a guest, with every privilege. Three deviations from the GDK documentation:

  1. XUserUnregisterForChangeEvent returns false for a token it doesn't hold (proved). user_unregister_change returns g_tokens.erase(token.token) != 0, so Unregister({0}, false), Unregister({999}, false) and a second unregister of the same token all return false. The GDK page says it "returns false if wait is false and the un-register is pending", and true otherwise, and its own polling pattern loops on wait=false until the call returns true. Nothing is ever pending offline, so returning true always would match that. As written, a title that polls after a duplicate or unknown token spins forever. The PR's test currently asserts the "true once, then false" behaviour.
  2. XUserIsStoreUser(nullptr) returns false (proved). It returns valid(user), but the parameter is _In_opt_, and the GDK page says the function always returns true on console. return true; would match.
  3. Slots answered with 0 when their signature is unknown (partly reasoned). private_slot<15> (and 47–49) log once and return 0 without writing anything. A probe calling slot 15 as (h, 16, buf, &used) got S_OK with buf and used left untouched. I think slot 15 is where the pre-component XUserGetGamertag(user, size, buf, used) sat before it moved to IXUserGamertagImpl, but that's an inference I couldn't check against an old header. Either way, S_OK from a slot whose outputs aren't filled lets a caller read uninitialised data. E_NOTIMPL for unknown-signature slots would fail visibly instead.

Smaller:

  • The comment at the handle magic (~L85–116) says it tells a live handle from a closed one. But user_close frees the block, so valid() on a stale handle reads freed memory. Once the heap reuses that block, a stale handle validates, and a title's double close would free someone else's handle. That's a title bug, so this is low severity, but the comment overstates it. Not reproduced here because the block wasn't reused.
  • A LEONIDA_GAMERTAG of 64+ characters makes getenv_s fail with ERANGE and silently falls back to "Player", while the PR text promises a message.

Checked and found correct:

  • Class and interface GUIDs, the IXUserImpl..6 chain and the 50-slot order, against xgameruntime-docs; the MSA-token Async/Result/ResultSize order.
  • IXUserGamertagImpl served as its own interface.
  • The E_GAMEUSER_* values and the XUserState, AgeGroup, GamertagComponent and AddOptions values; AllowGuests with AddDefaultUserSilently refused.
  • XAsync against the vendored AsyncLib: result size 8, handle created in GetResult, zero-payload results, and no block left waiting after a synchronous failure.
  • Gamertag sizes: terminator counted, one byte short → 0x8007007A, unknown component → E_INVALIDARG.
  • Open-handle accounting across duplicate and close.

…ndles)

From @CharlyRien's review of 056661d:
- XUserUnregisterForChangeEvent returns true for every token: false means
  "still pending", which never happens offline, so a title polling with
  wait = false spun on an unknown or repeated token.
- XUserIsStoreUser is true, also for a null handle (_In_opt_; documented
  as always true on console).
- Slot 15 (no public signature) answers E_NOTIMPL instead of S_OK with
  nothing written. XUserIsSignOutPresent (47) stays 0: it is read as a
  bool, and E_NOTIMPL's low byte would mean true.
- Handles are validated by lookup in a live set rather than by reading a
  magic in memory that may already be freed; a double close is a no-op.
- LEONIDA_GAMERTAG of any length reaches the "longer than 15" message
  (the length is queried first; 64+ characters fell back silently).

Tests: 42 checks; a new ctest run with a 70-character gamertag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@romainveurrier

Copy link
Copy Markdown
Contributor Author

@CharlyRien, thanks for the careful review and the MSVC run. All five points are fixed in b6213c9:

  1. XUserUnregisterForChangeEvent now returns true for every token, including ones already unregistered or never issued, since nothing is pending offline. The test now checks wait = false with a repeated token and with an unknown one.
  2. XUserIsStoreUser returns true, also for nullptr.
  3. Slot 15 answers E_NOTIMPL (and logs once). The test calls it with your probe's (h, 16, buf, &used) shape. I kept slot 47 (XUserIsSignOutPresent) at 0: it's read as a bool, and E_NOTIMPL's low byte (0x01) would mean "sign-out present". Slots 48 and 49 were already E_NOTIMPL. Your guess that 15 is the old pre-component XUserGetGamertag is plausible, but I can't confirm it either, so the docs still call it unknown.
  4. Handles: they're now checked against a set of live handles (looked up, never dereferenced), so a closed or foreign pointer is refused without touching memory, and a double close does nothing. The address can still be reused after a free. Then a stale handle names a newer live one, which is harmless here because every handle names the same user. The comment says so.
  5. LEONIDA_GAMERTAG: the length is queried first, so any length over 15 gets the message. There's a new ctest run with 70 characters.

42 checks; the four xgameruntime* tests pass under Wine (clang-cl 22.1.8). #3550 (XStoreImpl) has the same handle and unknown-slot patterns; I'm applying the same fixes there.

romainveurrier added a commit to romainveurrier/Leonida that referenced this pull request Nov 22, 2026
… slots

The same fixes @CharlyRien's review of ItsNotRexed#3363 led to for XUserImpl:
- context, license and product-query handles are looked up by kind in a
  live map, never read, so a closed, foreign or wrong-kind pointer is
  refused without touching freed memory, and a double close is a no-op;
- the docs' unknown slots 46-48 answer E_NOTIMPL instead of S_OK with
  nothing written; XStoreIsAvailabilityPurchasable (70) stays 0, since
  it is read as a bool and E_NOTIMPL's low byte would mean true.

Tests: 33 checks (double close, slot 46, slot 70).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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 b6213c9 merged onto main f847748 (clean merge), full build, full ctest -C Release -j 4: 100% tests passed out of 135 | new failures vs main: none. The main baseline on the same machine passes 132/132.

Coordination note: my XGameSaveImpl PR #3505 also goes in its own file and touches QueryApiImpl with one line at its top (rt::xgr::query_save(...)), so whichever of the two lands second needs a one-line rebase there, nothing more.

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

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head merged with main f847748, MSVC 14.51):

  • RTX 4080 SUPER: xgameruntime_user, xgameruntime_user_gamertag and xgameruntime_user_gamertag_long pass.
  • Intel UHD 770 (VK_LOADER_DRIVERS_DISABLE=*nv*): the same three pass.
  • Negative control: with xgameruntime.cpp reverted to main (so XUserImpl is no longer served), all three fail on both GPUs.

Only these three tests were built and run, not the full ctest. No concerns.

@Dom-in

Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Layout note (read only): I compared your 50-slot XUserImpl vtable with the LGPL Wine IDL (include/xuser.idl). The two agree on slots 3-14, then the IDL has no entry for the docs' _unknown_ slot 15, so all its later methods sit one slot earlier than yours (its chain is 33+2+3+1+4+3 = 46 methods). The same omission shows up for XDisplay's unknown slot 3 (#3476), and where the docs list no unknown slot (XGameSave, XGame, XStore) the two sources agree exactly. So the IDL just drops unknown methods, and your docs-based layout with slot 15 kept (E_NOTIMPL) is the right reading. Nothing to change. Writing it down so nobody "fixes" the layout from the IDL later.

AI-assisted review (Claude Code).

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.

5 participants