xgameruntime_gamesave: prevent uint64_t quota underflow in commit() on case-colliding blobs #147

Closed
Andretuta wants to merge 1 commit from upstream-pr/147 into main
Andretuta commented 2026-11-24 02:28:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-24T02:28:57Z.


Carried over from old PR 9880 by ChalanaGimhanaX in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9880, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
ChalanaGimhanaX: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Claimed in the coordination issue as: https://github.com/ProjectLeonida/Leonida/issues/9677#issuecomment-7116740917

(Resubmission of #8855, which was previously approved by @abnalstlll-byte and verified by @smowlyy before being closed during the repo transfer cleanup; cleanly rebased onto ProjectLeonida/Leonida main 7e39a81ea and re-verified.)

What and why

In runtime/shims/xgameruntime_gamesave.cpp, commit() computes the post-update quota by iterating u->ops and subtracting each existing file's size from uint64_t after = used_bytes(c->user_dir):

const uint64_t old = fs::is_regular_file(p, ec) ? static_cast<uint64_t>(fs::file_size(p, ec)) : 0;
after = after - old + (op.write ? op.data.size() : 0);

On Windows NTFS (case-insensitive), if a single XGameSaveUpdate references two blob names that differ only in ASCII case (e.g., deleting or updating "story" and "STORY" when "story" exists on disk), both paths resolve to the same existing file. Subtracting its size twice underflows uint64_t after to ~UINT64_MAX and causes a tiny update or even a pure delete to fail with a bogus E_GS_QUOTA_EXCEEDED. Likewise, if fs::file_size(p, ec) fails and sets ec, it returns static_cast<uintmax_t>(-1), which also wrapped after.

Changes:

  • In commit() (quota calculation loop only):
    • Check !size_ec && sz != static_cast<uintmax_t>(-1) before subtracting an existing file's size.
    • Track accounted paths and skip re-subtracting files where fs::equivalent(prev, p, eq_ec) is true so each on-disk file is subtracted at most once.
    • Use saturating subtraction and saturating addition on after.
  • Add regression checks in runtime/test/xgameruntime_gamesave_test.cpp for case-colliding deletes and write + case-colliding delete in a single update.

Verification (rebased onto 7e39a81ea)

Windows 11 x64, MSVC 19.50 (Visual Studio 18 Community), Windows SDK 10.0.26100.0, Release x64, CPU-only synthetic tests (no game files):

  • Negative control (new checks against unpatched main): xgameruntime_gamesave_test failed 4 of 66 checks (both case-colliding deletes and write+delete returned E_GS_QUOTA_EXCEEDED, leaving the blob on disk).
  • With fix (rebased on 7e39a81ea):
    • ctest --test-dir runtime/build -C Release -R ^xgameruntime_gamesave$ --output-on-failure -V: 1/1 passed (xgameruntime_gamesave_test: 66 checks, 0 failures, 0.16 sec)
    • python -I scripts/check_no_game_data.py runtime/shims/xgameruntime_gamesave.cpp runtime/test/xgameruntime_gamesave_test.cpp: exit 0
    • git diff origin/main..HEAD --check: exit 0 (LF only, 0 CRLF)

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. (N/A — offline XGameSave runtime shim fix, not a translator rule.)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5464945736 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/147). Originally posted by [Andretuta](https://github.com/Andretuta) on 2026-11-24T02:28:57Z. --- > Carried over from **old PR 9880** by `ChalanaGimhanaX` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9880`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `ChalanaGimhanaX`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): Claimed in the coordination issue as: https://github.com/ProjectLeonida/Leonida/issues/9677#issuecomment-7116740917 *(Resubmission of `#8855`, which was previously approved by `@abnalstlll-byte` and verified by `@smowlyy` before being closed during the repo transfer cleanup; cleanly rebased onto `ProjectLeonida/Leonida` `main` `7e39a81ea` and re-verified.)* ### What and why In `runtime/shims/xgameruntime_gamesave.cpp`, `commit()` computes the post-update quota by iterating `u->ops` and subtracting each existing file's size from `uint64_t after = used_bytes(c->user_dir)`: ```cpp const uint64_t old = fs::is_regular_file(p, ec) ? static_cast<uint64_t>(fs::file_size(p, ec)) : 0; after = after - old + (op.write ? op.data.size() : 0); ``` On Windows NTFS (case-insensitive), if a single `XGameSaveUpdate` references two blob names that differ only in ASCII case (e.g., deleting or updating `"story"` and `"STORY"` when `"story"` exists on disk), both paths resolve to the same existing file. Subtracting its size twice underflows `uint64_t after` to `~UINT64_MAX` and causes a tiny update or even a pure delete to fail with a bogus `E_GS_QUOTA_EXCEEDED`. Likewise, if `fs::file_size(p, ec)` fails and sets `ec`, it returns `static_cast<uintmax_t>(-1)`, which also wrapped `after`. Changes: - In `commit()` (quota calculation loop only): - Check `!size_ec && sz != static_cast<uintmax_t>(-1)` before subtracting an existing file's size. - Track accounted paths and skip re-subtracting files where `fs::equivalent(prev, p, eq_ec)` is true so each on-disk file is subtracted at most once. - Use saturating subtraction and saturating addition on `after`. - Add regression checks in `runtime/test/xgameruntime_gamesave_test.cpp` for case-colliding deletes and write + case-colliding delete in a single update. ### Verification (rebased onto `7e39a81ea`) Windows 11 x64, MSVC 19.50 (Visual Studio 18 Community), Windows SDK 10.0.26100.0, Release x64, CPU-only synthetic tests (no game files): - **Negative control** (new checks against unpatched `main`): `xgameruntime_gamesave_test` failed 4 of 66 checks (both case-colliding deletes and write+delete returned `E_GS_QUOTA_EXCEEDED`, leaving the blob on disk). - **With fix (rebased on `7e39a81ea`)**: - `ctest --test-dir runtime/build -C Release -R ^xgameruntime_gamesave$ --output-on-failure -V`: **1/1 passed** (`xgameruntime_gamesave_test: 66 checks, 0 failures`, `0.16 sec`) - `python -I scripts/check_no_game_data.py runtime/shims/xgameruntime_gamesave.cpp runtime/test/xgameruntime_gamesave_test.cpp`: exit 0 - `git diff origin/main..HEAD --check`: exit 0 (LF only, 0 CRLF) AI-assisted: yes ### Checklist - [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. *(N/A — offline XGameSave runtime shim fix, not a translator rule.)*
OreoFR commented 2026-11-29 17:00:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T17:00:16Z.


@ChalanaGimhanaX this carried-over PR (your old PR 9880) now conflicts with main (runtime/test/xgameruntime_gamesave_test.cpp). Could you merge main into it, by pushing to its branch or in your own PR from it (no force-push needed)? If there's no update within 24 h, OreoFR may carry it in a new PR that keeps you as co-author (per the maintainers' plan). — OreoFR's agent (AI-assisted)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7259343668 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/147#issuecomment-7259343668). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T17:00:16Z. --- @ChalanaGimhanaX this carried-over PR (your old PR 9880) now conflicts with `main` (`runtime/test/xgameruntime_gamesave_test.cpp`). Could you merge `main` into it, by pushing to its branch or in your own PR from it (no force-push needed)? If there's no update within 24 h, OreoFR may carry it in a new PR that keeps you as co-author (per the maintainers' plan). — OreoFR's agent (AI-assisted)
aslan2142 commented 2026-12-01 12:11:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T12:11:30Z.


Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review.

(AI-assisted triage.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7310781066 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/147#issuecomment-7310781066). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T12:11:30Z. --- Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review. *(AI-assisted triage.)*
aslan2142 commented 2026-12-01 16:26:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:26:08Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317600511 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/147#issuecomment-7317600511). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:26:08Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
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
ProjectLeonida/mandy!147
No description provided.