runtime: harden save_blob_pack size checks and add unit fixture #1885

Merged
Asimmimx merged 1 commit from upstream-pr/1892 into main 2026-11-24 20:55:15 +00:00
Asimmimx commented 2026-11-24 20:21:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Asimmimx on 2026-11-24T20:21:54Z.


Summary

Hardens x2w_save_pack::bytes_needed() in src/startup_stubs/save_blob_pack.h and adds a standalone, runtime-free unit fixture ( ests/startup_save_blob_pack_check.cpp) with a WILL_FAIL negative control.

Two bugs existed in the previous ytes_needed() check:

  1. Unsigned wraparound: extra = name.size() + 1 + bytes.size() followed by extra < name.size() missed ytes.size() == SIZE_MAX, because
    ame.size() + 1 + SIZE_MAX wraps modulo 2^64 back to
    ame.size().
  2. Silent uint32_t truncation: BlobInfo::size is std::uint32_t and write() casts piece.bytes.size() to std::uint32_t, but ytes_needed() accepted payloads larger than UINT32_MAX.

Changes

  • src/startup_stubs/save_blob_pack.h:
    • Factor overflow-safe per-piece accounting into detail::add_piece_size(), refusing data_bytes > UINT32_MAX and checking
      ame_bytes >= SIZE_MAX - data_bytes before adding.
    • Add a ytes_needed(const PieceSize*, std::size_t, std::size_t&) overload sharing the same accounting so boundary and overflow cases can be verified without allocating >4 GiB.
    • Return early in write() when pieces.empty().
  • ests/startup_save_blob_pack_check.cpp & CMakeLists.txt:
  • Register startup_save_blob_pack (3 packed layouts with guard bytes and embedded-NUL payloads, 3 acceptance boundaries, 7 refusal cases preserving out) and startup_save_blob_pack_legacy_size_control (--mutate legacy-size, WILL_FAIL TRUE).
  • docs/startup-runtime.md: document the startup_save_blob_pack CTest command next to startup_gamesave_read_async_.

Verification

Base commit: 8c82879f (PR commit: cf27add8).

powershell cmake --preset dev cmake --build --preset dev --target startup_save_blob_pack_check startup_gamesave_read_async startup_stub_gameinput startup_stub_consumer ctest --preset dev --no-tests=error -R '^startup_(save_blob_pack|gamesave_read_async_)' -V .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py

Observed results:

  • startup_save_blob_pack: Passed (PASS (3 pack layouts, 3 boundaries, 7 refusals))
  • startup_save_blob_pack_legacy_size_control: Passed (WILL_FAIL TRUE; failed as expected on payload exceeds uint32_t: expected refusal, got 4294967325 and data_bytes SIZE_MAX wraparound: expected refusal, got 32)
  • startup_gamesave_read_async_controlled: Passed
  • startup_gamesave_read_async_system: Passed
  • ests/docs_links_check.py: Passed (18 Markdown files, 376 links)
  • ests/bidi_controls_check.py: Passed (1330 tracked files)

Capability skips: none. Scope: verifies save_blob_pack.h and the existing startup_gamesave_read_async_* fixtures only; does not establish full-game startup.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6590663105 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1892). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-24T20:21:54Z. --- ### Summary Hardens x2w_save_pack::bytes_needed() in src/startup_stubs/save_blob_pack.h and adds a standalone, runtime-free unit fixture ( ests/startup_save_blob_pack_check.cpp) with a WILL_FAIL negative control. Two bugs existed in the previous ytes_needed() check: 1. **Unsigned wraparound:** extra = name.size() + 1 + bytes.size() followed by extra < name.size() missed ytes.size() == SIZE_MAX, because ame.size() + 1 + SIZE_MAX wraps modulo 2^64 back to ame.size(). 2. **Silent uint32_t truncation:** BlobInfo::size is std::uint32_t and write() casts piece.bytes.size() to std::uint32_t, but ytes_needed() accepted payloads larger than UINT32_MAX. ### Changes - src/startup_stubs/save_blob_pack.h: - Factor overflow-safe per-piece accounting into detail::add_piece_size(), refusing data_bytes > UINT32_MAX and checking ame_bytes >= SIZE_MAX - data_bytes before adding. - Add a ytes_needed(const PieceSize*, std::size_t, std::size_t&) overload sharing the same accounting so boundary and overflow cases can be verified without allocating >4 GiB. - Return early in write() when pieces.empty(). - ests/startup_save_blob_pack_check.cpp & CMakeLists.txt: - Register startup_save_blob_pack (3 packed layouts with guard bytes and embedded-NUL payloads, 3 acceptance boundaries, 7 refusal cases preserving out) and startup_save_blob_pack_legacy_size_control (--mutate legacy-size, WILL_FAIL TRUE). - docs/startup-runtime.md: document the startup_save_blob_pack CTest command next to startup_gamesave_read_async_. ### Verification Base commit: 8c82879f (PR commit: cf27add8). ``powershell cmake --preset dev cmake --build --preset dev --target startup_save_blob_pack_check startup_gamesave_read_async startup_stub_gameinput startup_stub_consumer ctest --preset dev --no-tests=error -R '^startup_(save_blob_pack|gamesave_read_async_)' -V .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py `` Observed results: - startup_save_blob_pack: **Passed** (PASS (3 pack layouts, 3 boundaries, 7 refusals)) - startup_save_blob_pack_legacy_size_control: **Passed** (WILL_FAIL TRUE; failed as expected on payload exceeds uint32_t: expected refusal, got 4294967325 and data_bytes SIZE_MAX wraparound: expected refusal, got 32) - startup_gamesave_read_async_controlled: **Passed** - startup_gamesave_read_async_system: **Passed** - ests/docs_links_check.py: **Passed** (18 Markdown files, 376 links) - ests/bidi_controls_check.py: **Passed** (1330 tracked files) Capability skips: none. Scope: verifies save_blob_pack.h and the existing startup_gamesave_read_async_* fixtures only; does not establish full-game startup.
upstream-sync closed this pull request 2026-11-24 21:01:09 +00:00
tedin7 commented 2026-11-24 21:01:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T20:27:24Z.

Review: COMMENTED


Review of PR #1892 at exact head cf27add80c25d59fc88733bc04f81cf2a77dc62d (4 files: CMakeLists.txt, docs/startup-runtime.md, src/startup_stubs/save_blob_pack.h, tests/startup_save_blob_pack_check.cpp). Comment only, no approval.

Verdict: logic is correct and the fix is real; no P1. Two P2 test/design gaps, worth fixing before merge.

What I checked

  • Arithmetic in detail::add_piece_size (save_blob_pack.h:35): the data_bytes > UINT32_MAX cap runs first, so SIZE_MAX - data_bytes cannot underflow. name_bytes >= SIZE_MAX - data_bytes rejects exactly when name+data+1 would wrap. extra > SIZE_MAX - bytes rejects total overflow. No off-by-one found; the exact-SIZE_MAX acceptance case is right.
  • The cap matches BlobInfo::size being uint32_t: before, write() silently truncated the size of a payload over 4 GiB while still copying all of it. Now snapshot_read fails with e_fail (runtime_proxy.cpp:593) before any buffer is written. Good fix.
  • write() early return on empty pieces is safe; the only caller is guarded by state->bytes <= buffer_size and a non-null buffer.
  • Clean-room / title-neutral: no title names, no game data, no copied vendor text in the diff. Docs sentence is neutral.

P2 findings

  1. tests/startup_save_blob_pack_check.cpp:162 does not prove the count > SIZE_MAX / sizeof(Blob) guard (save_blob_pack.h:50). The case passes count = SIZE_MAX/24+1 with a pointer to ONE element. If the guard is deleted, the loop reads sizes[1..] out of bounds, but the stale stack data still happens to push the total over the limit and the test still passes. Mutation 4 below shows it. Passing a real overflow must not rely on out-of-bounds reads. Suggest a count that would wrap count*24 and a sizes array whose early entries are all valid and tiny, or at least a sentinel check that no element past the first is read.
  2. The production overload is never tested for refusal. bytes_needed(const std::vector<Piece>&, ...) (save_blob_pack.h:61) is the only one runtime_proxy.cpp:593 calls. The PieceSize overload (line 48) is used only by the test, so it is production code that exists for tests (see repo test rules). Mutation 5 below, which removes the shared check from the vector overload only, still passes. Both overloads share add_piece_size, so the risk is small today, but the test would not notice the vector overload drifting. Either make the vector overload call the PieceSize one (build a size list from pieces) so one tested path serves both, or keep the sizes overload in the test file only.

Smaller notes (not blocking)

  • The legacy-size negative control re-implements the old check inside the test. It does fail the new fixture (2 failures), but it only guards "the old code was weaker", not a future regression in the real header. Mutations 1 to 3 and 6 show the real header is covered for those lines, so this is fine.
  • "name + 1 + data overflow" (name=SIZE_MAX-10, data=10) is also refused by the legacy check, so it does not separate old from new; "payload exceeds uint32_t" and "data_bytes SIZE_MAX wraparound" are the two that do.
  • std::size_t{UINT32_MAX} + 1 wraps to 0 on a 32-bit size_t; fine for the x64 targets here, but the fixture would mis-test on a 32-bit build.

Commands executed locally

git fetch origin pull/1892/head:rev-c1-1892 ; worktree at cf27add8 (matches gh headRefOid)
g++ -std=c++17 -Wall -Wextra -Isrc tests/startup_save_blob_pack_check.cpp
  -> no warnings; ./check -> "startup_save_blob_pack: PASS (3 pack layouts, 3 boundaries, 7 refusals)"  rc=0
./check --mutate legacy-size -> FAIL payload exceeds uint32_t ... FAIL data_bytes SIZE_MAX wraparound ... 2 FAILED  rc=1 (expected)
python3 tests/docs_links_check.py   -> Self-test passed. Checked 18 Markdown files and 376 links.
python3 tests/bidi_controls_check.py -> Self-test passed. Checked 1331 tracked files: no bidirectional controls.
git diff --check origin/main...HEAD -> clean
x2w_pipe.py win2025 1892 --targets startup_save_blob_pack_check,startup_stub_consumer,xmem_shim --regex '^(startup_save_blob_pack|startup_save_blob_pack_legacy_size_control|startup_storage_directory_batch)$'
  -> Windows Server 2025, MSVC 14.44, Debug and Release /O2: 3/3 passed each (X2W_EXIT=0)

Mutations on the real header (each reverted with git checkout -- .)

# Mutation Result
1 remove data_bytes > UINT32_MAX cap caught: "payload exceeds uint32_t"
2 >= to > in name_bytes >= SIZE_MAX - data_bytes caught: "name + 1 + data overflow"
3 remove extra > SIZE_MAX - bytes caught: "total + extra overflow by 1", "two pieces overflowing total"
4 remove count > SIZE_MAX / sizeof(Blob) (sizes overload) NOT caught, test still PASS (P2 #1)
5 vector overload: replace checked add with plain sum NOT caught, test still PASS (P2 #2)
6 stop writing the NUL after each name caught: "name not NUL-terminated"

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046832252 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1892#pullrequestreview-6046832252). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T20:27:24Z. **Review:** COMMENTED --- Review of PR #1892 at exact head `cf27add80c25d59fc88733bc04f81cf2a77dc62d` (4 files: CMakeLists.txt, docs/startup-runtime.md, src/startup_stubs/save_blob_pack.h, tests/startup_save_blob_pack_check.cpp). Comment only, no approval. **Verdict: logic is correct and the fix is real; no P1. Two P2 test/design gaps, worth fixing before merge.** ### What I checked - Arithmetic in `detail::add_piece_size` (save_blob_pack.h:35): the `data_bytes > UINT32_MAX` cap runs first, so `SIZE_MAX - data_bytes` cannot underflow. `name_bytes >= SIZE_MAX - data_bytes` rejects exactly when `name+data+1` would wrap. `extra > SIZE_MAX - bytes` rejects total overflow. No off-by-one found; the exact-`SIZE_MAX` acceptance case is right. - The cap matches `BlobInfo::size` being `uint32_t`: before, `write()` silently truncated the size of a payload over 4 GiB while still copying all of it. Now `snapshot_read` fails with `e_fail` (runtime_proxy.cpp:593) before any buffer is written. Good fix. - `write()` early return on empty `pieces` is safe; the only caller is guarded by `state->bytes <= buffer_size` and a non-null buffer. - Clean-room / title-neutral: no title names, no game data, no copied vendor text in the diff. Docs sentence is neutral. ### P2 findings 1. **tests/startup_save_blob_pack_check.cpp:162 does not prove the `count > SIZE_MAX / sizeof(Blob)` guard** (save_blob_pack.h:50). The case passes `count = SIZE_MAX/24+1` with a pointer to ONE element. If the guard is deleted, the loop reads `sizes[1..]` out of bounds, but the stale stack data still happens to push the total over the limit and the test still passes. Mutation 4 below shows it. Passing a real overflow must not rely on out-of-bounds reads. Suggest a `count` that would wrap `count*24` and a `sizes` array whose early entries are all valid and tiny, or at least a sentinel check that no element past the first is read. 2. **The production overload is never tested for refusal.** `bytes_needed(const std::vector<Piece>&, ...)` (save_blob_pack.h:61) is the only one `runtime_proxy.cpp:593` calls. The `PieceSize` overload (line 48) is used only by the test, so it is production code that exists for tests (see repo test rules). Mutation 5 below, which removes the shared check from the vector overload only, still passes. Both overloads share `add_piece_size`, so the risk is small today, but the test would not notice the vector overload drifting. Either make the vector overload call the `PieceSize` one (build a size list from pieces) so one tested path serves both, or keep the sizes overload in the test file only. ### Smaller notes (not blocking) - The `legacy-size` negative control re-implements the old check inside the test. It does fail the new fixture (2 failures), but it only guards "the old code was weaker", not a future regression in the real header. Mutations 1 to 3 and 6 show the real header is covered for those lines, so this is fine. - "name + 1 + data overflow" (name=`SIZE_MAX-10`, data=10) is also refused by the legacy check, so it does not separate old from new; "payload exceeds uint32_t" and "data_bytes SIZE_MAX wraparound" are the two that do. - `std::size_t{UINT32_MAX} + 1` wraps to 0 on a 32-bit `size_t`; fine for the x64 targets here, but the fixture would mis-test on a 32-bit build. ### Commands executed locally ``` git fetch origin pull/1892/head:rev-c1-1892 ; worktree at cf27add8 (matches gh headRefOid) g++ -std=c++17 -Wall -Wextra -Isrc tests/startup_save_blob_pack_check.cpp -> no warnings; ./check -> "startup_save_blob_pack: PASS (3 pack layouts, 3 boundaries, 7 refusals)" rc=0 ./check --mutate legacy-size -> FAIL payload exceeds uint32_t ... FAIL data_bytes SIZE_MAX wraparound ... 2 FAILED rc=1 (expected) python3 tests/docs_links_check.py -> Self-test passed. Checked 18 Markdown files and 376 links. python3 tests/bidi_controls_check.py -> Self-test passed. Checked 1331 tracked files: no bidirectional controls. git diff --check origin/main...HEAD -> clean x2w_pipe.py win2025 1892 --targets startup_save_blob_pack_check,startup_stub_consumer,xmem_shim --regex '^(startup_save_blob_pack|startup_save_blob_pack_legacy_size_control|startup_storage_directory_batch)$' -> Windows Server 2025, MSVC 14.44, Debug and Release /O2: 3/3 passed each (X2W_EXIT=0) ``` ### Mutations on the real header (each reverted with `git checkout -- .`) | # | Mutation | Result | |---|---|---| | 1 | remove `data_bytes > UINT32_MAX` cap | caught: "payload exceeds uint32_t" | | 2 | `>=` to `>` in `name_bytes >= SIZE_MAX - data_bytes` | caught: "name + 1 + data overflow" | | 3 | remove `extra > SIZE_MAX - bytes` | caught: "total + extra overflow by 1", "two pieces overflowing total" | | 4 | remove `count > SIZE_MAX / sizeof(Blob)` (sizes overload) | **NOT caught, test still PASS** (P2 #1) | | 5 | vector overload: replace checked add with plain sum | **NOT caught, test still PASS** (P2 #2) | | 6 | stop writing the NUL after each name | caught: "name not NUL-terminated" | AI-assisted; commands listed were executed locally.
ChazY11 commented 2026-11-24 21:01:24 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T20:48:36Z.


Built the fixture and re-broke both reported bugs, plus a third check

At the PR head, Windows 11 build 26300.9974, MSVC 19.44:

cmake --preset dev
cmake --build --preset dev --target startup_save_blob_pack_check
ctest --preset dev -R 'save_blob_pack'
1/2 Test #165: startup_save_blob_pack .......................   Passed 0.16 sec
2/2 Test #166: startup_save_blob_pack_legacy_size_control ...   Passed 0.02 sec
100% tests passed, 0 tests failed out of 2

Three mutations to src/startup_stubs/save_blob_pack.h, each reverted:

mutation result
if (data_bytes > UINT32_MAX) return false; → if (false) caught
if (name_bytes >= SIZE_MAX - data_bytes) return false; → if (false) caught
if (extra > SIZE_MAX - bytes) return false; → if (false) caught

The first two are exactly the two bugs the description names, so the fixture pins them individually rather than as a block. The
third was already present and the new cases cover it too — worth knowing, since it means the fixture is testing the whole
bytes_needed() guard set and not just the two additions.

I find the SIZE_MAX one the interesting case: name.size() + 1 + SIZE_MAX wrapping back to name.size() is only reachable
with bytes.size() == SIZE_MAX, and a synthetic BlobInfo can express it without allocating anything. That is the right kind
of negative control for an integer bug — no allocation, no UB, one input that would wrap.

The WILL_FAIL control

startup_save_blob_pack_legacy_size_control runs the same executable with --mutate legacy-size, and CMake marks it
WILL_FAIL. That gives the fixture its own proof that the assertions can fail — the pattern the repo uses elsewhere, and it
is what makes "the test passes" mean something here.

One question

bytes_needed() now refuses data_bytes > UINT32_MAX, matching the write() cast. Does write() still cast silently for a
caller that reaches it by another route (for example a piece built directly rather than through bytes_needed())? If the only
entry point is the checked path, this is airtight; if a public overload exists, a debug-only assert next to the cast would keep
the two in step.

Verdict

Reviewed. Both reported bugs are pinned by their own mutation, the overflow guard already in place is pinned too, and the
self-failing control makes the fixture honest. No objections.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7144085420 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1892#issuecomment-7144085420). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T20:48:36Z. --- ## Built the fixture and re-broke both reported bugs, plus a third check At the PR head, Windows 11 build 26300.9974, MSVC 19.44: ```powershell cmake --preset dev cmake --build --preset dev --target startup_save_blob_pack_check ctest --preset dev -R 'save_blob_pack' 1/2 Test #165: startup_save_blob_pack ....................... Passed 0.16 sec 2/2 Test #166: startup_save_blob_pack_legacy_size_control ... Passed 0.02 sec 100% tests passed, 0 tests failed out of 2 ``` Three mutations to `src/startup_stubs/save_blob_pack.h`, each reverted: | mutation | result | |---|---| | `if (data_bytes > UINT32_MAX) return false;` → `if (false)` | **caught** | | `if (name_bytes >= SIZE_MAX - data_bytes) return false;` → `if (false)` | **caught** | | `if (extra > SIZE_MAX - bytes) return false;` → `if (false)` | **caught** | The first two are exactly the two bugs the description names, so the fixture pins them individually rather than as a block. The third was already present and the new cases cover it too — worth knowing, since it means the fixture is testing the whole `bytes_needed()` guard set and not just the two additions. I find the `SIZE_MAX` one the interesting case: `name.size() + 1 + SIZE_MAX` wrapping back to `name.size()` is only reachable with `bytes.size() == SIZE_MAX`, and a synthetic `BlobInfo` can express it without allocating anything. That is the right kind of negative control for an integer bug — no allocation, no UB, one input that would wrap. ## The WILL_FAIL control `startup_save_blob_pack_legacy_size_control` runs the same executable with `--mutate legacy-size`, and CMake marks it `WILL_FAIL`. That gives the fixture its own proof that the *assertions* can fail — the pattern the repo uses elsewhere, and it is what makes "the test passes" mean something here. ## One question `bytes_needed()` now refuses `data_bytes > UINT32_MAX`, matching the `write()` cast. Does `write()` still cast silently for a caller that reaches it by another route (for example a piece built directly rather than through `bytes_needed()`)? If the only entry point is the checked path, this is airtight; if a public overload exists, a debug-only assert next to the cast would keep the two in step. ## Verdict Reviewed. Both reported bugs are pinned by their own mutation, the overflow guard already in place is pinned too, and the self-failing control makes the fixture honest. No objections. ## Notes - AI-assisted review.
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
R0Y00/Xbox2Windows!1885
No description provided.