fix(chans): preserve wrapped hex-count rounding without signed overflow #1048

Merged
coah80 merged 1 commits from agent/bittle/chans-rounding-review into main 2026-10-03 05:01:49 +00:00
coah80 commented 2026-10-03 05:01:21 +00:00 (Migrated from github.com)

Reachable source-width correction

Both hexadecimal-size passes in VmBlobPackCommon evaluated (count + 1) / 2 in signed 32-bit arithmetic. The existing parser accepts H2147483647, so that addition has signed-overflow UB. Perform only the addition in u32, convert the wrapped word to s32, then preserve the existing signed division and all surrounding error checks.

The original PPC produces -1073741824 at INT_MAX; this patch preserves that result and the signed 64-bit argument passed to the existing space check. It does not replace it with positive rounding. The out-of-range u32-to-s32 conversion is implementation-defined in C; configured MWCC and host GCC bit-preserving behavior were verified. No universal C-portability claim or fuzzy gain.

Independent verification

Rebased and tested on 409ef82dd8fe5f2416540b7b3037451b0cd43a33; tree 7c50ee9bae.

  • Source-extracted baseline UBSan reproduces INT_MAX+1 overflow
  • Candidate passes 1,000,022 arithmetic cases and 11 parser cases, including H2147483647, neighboring limits and H*
  • Two original-DOL instruction-slice interpreters each pass1,000,013 cases against widened independent arithmetic, including signed extension
  • Whole CHANSVm.o byte-identical; complete 1,027-unit report unchanged; all 1,027 allocated payloads/canonical relocations unchanged
  • All 222 exact functions independently compared; unit 222/233 exact, 38136/53564 matched code, 6904/6904 matched data preserved
  • Pool 125/125 identical; literal audit 233 functions/40 arguments, no candidates/skips/errors
  • Full 43U all_source/progress/report/DOL passed; SHA1 26116613f624061ba99c8d1a299aaa6efa85670d
  • Worktree and diff checks pass

These are focused arithmetic/parser/original-slice tests, not a complete VM or Wii runtime test. Detailed evidence and separately rejected union trial: tools/decomp-assist/CHANSVm-pack-rounding-20261003.md. No linking flags or other regions changed.

Note

Fix signed-overflow in VmBlobPackCommon hex-count rounding in CHANS VM

Changes the two hexadecimal-string size calculations in VmBlobPackCommon from signed count + 1 arithmetic to unsigned 32-bit addition converted to s32 before division. The sizing pass and writing pass now match. For ordinary counts the result is unchanged; at overflowing boundary values the wrapped result matches the original binary's size and space checks. Adds validation notes in CHANSVm-pack-rounding-20261003.md. Risk: none identified; behavior only differs at counts that previously triggered signed-overflow UB in CHANSVm.c.

Macroscope summarized 9655379.

## Reachable source-width correction Both hexadecimal-size passes in VmBlobPackCommon evaluated (count + 1) / 2 in signed 32-bit arithmetic. The existing parser accepts H2147483647, so that addition has signed-overflow UB. Perform only the addition in u32, convert the wrapped word to s32, then preserve the existing signed division and all surrounding error checks. The original PPC produces -1073741824 at INT_MAX; this patch preserves that result and the signed 64-bit argument passed to the existing space check. It does not replace it with positive rounding. The out-of-range u32-to-s32 conversion is implementation-defined in C; configured MWCC and host GCC bit-preserving behavior were verified. No universal C-portability claim or fuzzy gain. ## Independent verification Rebased and tested on 409ef82dd8fe5f2416540b7b3037451b0cd43a33; tree 7c50ee9bae79f54b59b78060415b2e005365494c. - Source-extracted baseline UBSan reproduces INT_MAX+1 overflow - Candidate passes 1,000,022 arithmetic cases and 11 parser cases, including H2147483647, neighboring limits and H* - Two original-DOL instruction-slice interpreters each pass1,000,013 cases against widened independent arithmetic, including signed extension - Whole CHANSVm.o byte-identical; complete 1,027-unit report unchanged; all 1,027 allocated payloads/canonical relocations unchanged - All 222 exact functions independently compared; unit 222/233 exact, 38136/53564 matched code, 6904/6904 matched data preserved - Pool 125/125 identical; literal audit 233 functions/40 arguments, no candidates/skips/errors - Full 43U all_source/progress/report/DOL passed; SHA1 26116613f624061ba99c8d1a299aaa6efa85670d - Worktree and diff checks pass These are focused arithmetic/parser/original-slice tests, not a complete VM or Wii runtime test. Detailed evidence and separately rejected union trial: tools/decomp-assist/CHANSVm-pack-rounding-20261003.md. No linking flags or other regions changed. <!-- Macroscope's pull request summary starts here --> <!-- Macroscope will only edit the content between these invisible markers, and the markers themselves will not be visible in the GitHub rendered markdown. --> <!-- If you delete either of the start / end markers from your PR's description, Macroscope will append its summary at the bottom of the description. --> > [!NOTE] > ### Fix signed-overflow in `VmBlobPackCommon` hex-count rounding in CHANS VM > Changes the two hexadecimal-string size calculations in `VmBlobPackCommon` from signed `count + 1` arithmetic to unsigned 32-bit addition converted to `s32` before division. The sizing pass and writing pass now match. For ordinary counts the result is unchanged; at overflowing boundary values the wrapped result matches the original binary's size and space checks. Adds validation notes in [CHANSVm-pack-rounding-20261003.md](https://github.com/coah80/wii-ipl/pull/1048/files#diff-a1c8eeda491bb1f9bd6b95cc34fbefa0803fbbe0b50c4055eb04526274aea8d8). Risk: none identified; behavior only differs at counts that previously triggered signed-overflow UB in [CHANSVm.c](https://github.com/coah80/wii-ipl/pull/1048/files#diff-fcd94241dcc82f12e50e6a224211f94b32d8c83c08ab0880c20261cea99b4f75). > > <!-- Macroscope's review summary starts here --> > > <sup><a href="https://app.macroscope.com">Macroscope</a> summarized 9655379.</sup> > <!-- Macroscope's review summary ends here --> > <!-- Macroscope's pull request summary ends here -->
coah80 commented 2026-10-03 05:01:47 +00:00 (Migrated from github.com)

Reviewed and merged: rebased on main, full build ok, DOL sha1 26116613f624061ba99c8d1a299aaa6efa85670d, 0 regressions, 0 forbidden patterns.

Reviewed and merged: rebased on main, full build ok, DOL sha1 26116613f624061ba99c8d1a299aaa6efa85670d, 0 regressions, 0 forbidden patterns.
Sign in to join this conversation.