runtime: harden save_blob_pack size checks and add unit fixture #1885
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1892"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
ame.size() + 1 + SIZE_MAX wraps modulo 2^64 back to
ame.size().
Changes
ame_bytes >= SIZE_MAX - data_bytes before adding.
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.pyObserved results:
Capability skips: none. Scope: verifies save_blob_pack.h and the existing startup_gamesave_read_async_* fixtures only; does not establish full-game startup.
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
detail::add_piece_size(save_blob_pack.h:35): thedata_bytes > UINT32_MAXcap runs first, soSIZE_MAX - data_bytescannot underflow.name_bytes >= SIZE_MAX - data_bytesrejects exactly whenname+data+1would wrap.extra > SIZE_MAX - bytesrejects total overflow. No off-by-one found; the exact-SIZE_MAXacceptance case is right.BlobInfo::sizebeinguint32_t: before,write()silently truncated the size of a payload over 4 GiB while still copying all of it. Nowsnapshot_readfails withe_fail(runtime_proxy.cpp:593) before any buffer is written. Good fix.write()early return on emptypiecesis safe; the only caller is guarded bystate->bytes <= buffer_sizeand a non-null buffer.P2 findings
count > SIZE_MAX / sizeof(Blob)guard (save_blob_pack.h:50). The case passescount = SIZE_MAX/24+1with a pointer to ONE element. If the guard is deleted, the loop readssizes[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 acountthat would wrapcount*24and asizesarray whose early entries are all valid and tiny, or at least a sentinel check that no element past the first is read.bytes_needed(const std::vector<Piece>&, ...)(save_blob_pack.h:61) is the only oneruntime_proxy.cpp:593calls. ThePieceSizeoverload (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 shareadd_piece_size, so the risk is small today, but the test would not notice the vector overload drifting. Either make the vector overload call thePieceSizeone (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)
legacy-sizenegative 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.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} + 1wraps to 0 on a 32-bitsize_t; fine for the x64 targets here, but the fixture would mis-test on a 32-bit build.Commands executed locally
Mutations on the real header (each reverted with
git checkout -- .)data_bytes > UINT32_MAXcap>=to>inname_bytes >= SIZE_MAX - data_bytesextra > SIZE_MAX - bytescount > SIZE_MAX / sizeof(Blob)(sizes overload)AI-assisted; commands listed were executed locally.
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:
Three mutations to
src/startup_stubs/save_blob_pack.h, each reverted:if (data_bytes > UINT32_MAX) return false;→if (false)if (name_bytes >= SIZE_MAX - data_bytes) return false;→if (false)if (extra > SIZE_MAX - bytes) return false;→if (false)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_MAXone the interesting case:name.size() + 1 + SIZE_MAXwrapping back toname.size()is only reachablewith
bytes.size() == SIZE_MAX, and a syntheticBlobInfocan express it without allocating anything. That is the right kindof 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_controlruns the same executable with--mutate legacy-size, and CMake marks itWILL_FAIL. That gives the fixture its own proof that the assertions can fail — the pattern the repo uses elsewhere, and itis what makes "the test passes" mean something here.
One question
bytes_needed()now refusesdata_bytes > UINT32_MAX, matching thewrite()cast. Doeswrite()still cast silently for acaller that reaches it by another route (for example a piece built directly rather than through
bytes_needed())? If the onlyentry 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