test: refuse a site too far from the host_cpu stub #1850

Merged
ChazY11 merged 1 commit from upstream-pr/1857 into main 2026-11-24 21:20:09 +00:00
ChazY11 commented 2026-11-24 19:39:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T19:39:43Z.


What

plan_replacement in tools/startup/host_cpu.py answers None when either jmp rel32 field would not fit:

forward = stub_address - (site + 5)
if not -2**31 <= forward < 2**31:
    return None

No check reached that branch — replacing it with if False left startup_host_cpu green — so a site more than 2 GiB from
the stub would be encoded with a wrapped offset instead of being refused.

Change

One case next to the existing rewrite fixtures:

code = bytes.fromhex("0f01fc9090")          # clzero plus the two nops it has to move
near = 0x1000
assert host_cpu.plan_replacement(code, near, 3, b"\x90" * 8, near + 0x100) is not None
assert host_cpu.plan_replacement(code, 0x7FFF0000_0000, 3, b"\x90" * 8, 0x1000) is None

The near plan is the guard against a fixture that passes by refusing everything.

Tested

out/pe-env/Scripts/python.exe -B tests/startup_host_cpu_check.py
with the case, check intact        exit 0
forward range check -> if False    exit 1   (the new case reports the encoded offset)
reverted                           exit 0

What I left alone, and why

The second range check (back, the jump from the moved instructions to just after the site) cannot be reached with an
input a caller would produce: it needs len(stub) > 2**31, i.e. a 2 GiB stub. I asserted only the forward field and said so
in the code comment, rather than writing an input that exists only to make a line run.

I also recorded a heavier lesson in the description of the earlier probes: mutating these offsets by hand can make the
patched code jump in a circle, so the fixture hangs instead of failing. My first run of this pair hung for minutes; the
runs behind the numbers above were made through a harness with a 90-second cap.

Notes

  • AI-assisted.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6589905905 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1857). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T19:39:43Z. --- ## What `plan_replacement` in `tools/startup/host_cpu.py` answers `None` when either `jmp rel32` field would not fit: ```python forward = stub_address - (site + 5) if not -2**31 <= forward < 2**31: return None ``` No check reached that branch — replacing it with `if False` left `startup_host_cpu` green — so a site more than 2 GiB from the stub would be encoded with a wrapped offset instead of being refused. ## Change One case next to the existing rewrite fixtures: ```python code = bytes.fromhex("0f01fc9090") # clzero plus the two nops it has to move near = 0x1000 assert host_cpu.plan_replacement(code, near, 3, b"\x90" * 8, near + 0x100) is not None assert host_cpu.plan_replacement(code, 0x7FFF0000_0000, 3, b"\x90" * 8, 0x1000) is None ``` The near plan is the guard against a fixture that passes by refusing everything. ## Tested ```powershell out/pe-env/Scripts/python.exe -B tests/startup_host_cpu_check.py ``` ```text with the case, check intact exit 0 forward range check -> if False exit 1 (the new case reports the encoded offset) reverted exit 0 ``` ## What I left alone, and why The second range check (`back`, the jump from the moved instructions to just after the site) **cannot be reached** with an input a caller would produce: it needs `len(stub) > 2**31`, i.e. a 2 GiB stub. I asserted only the forward field and said so in the code comment, rather than writing an input that exists only to make a line run. I also recorded a heavier lesson in the description of the earlier probes: mutating these offsets by hand can make the patched code jump in a circle, so the fixture **hangs** instead of failing. My first run of this pair hung for minutes; the runs behind the numbers above were made through a harness with a 90-second cap. ## Notes - AI-assisted.
tedin7 commented 2026-11-24 20:01:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:45:56Z.

Review: COMMENTED


Peer review of head cd2b333c079091dcdc7e863dc556520e1f643505 (PRODUCTION_REVIEW_ONLY, technical comment only).

No P1. One P2: the new test checks "far" but not the limit the title is about.

The change adds only a test (plan_ranges in tests/startup_host_cpu_check.py, +23 lines). I read plan_replacement in tools/startup/host_cpu.py: both range checks (forward = stub_address - (site + 5) and, for sites of 5 bytes or more, back) use -2**31 <= x < 2**31, which is the correct signed rel32 range. The near case and the far case in the test give the expected results (near: a plan; a site at 0x7FFF00000000 with the stub at 0x1000: refused).

What the test does not pin, shown by one-line changes to the forward check in tools/startup/host_cpu.py, each run against the check and then restored with git checkout -- .:

  • the check removed (if False:): the check fails (rc=1), but through a struct.error crash from the unguarded struct.pack, not through the new assertion. A version that wrapped the offset instead of crashing would pass.
  • range widened to -2**40 <= forward < 2**40: rc=0, survives.
  • upper bound made inclusive (forward <= 2**31): rc=0, survives.
  • only non-negative offsets accepted (0 <= forward < 2**31): rc=0, survives. The near case places the stub above the site, so a stub below the site is never exercised.
    The only far input is about 140 TiB away, so any threshold between 2 GiB and that distance passes. I suggest adding the four boundary values: forward = 231 - 1 (accepted), 231 (refused), -231 (accepted), -231 - 1 (refused). Placing the stub below the site would also cover the negative sign. For reference, plan_replacement at those sites behaves as expected today: for example a site at 0x80001004 with the stub at 0x1000 is refused.

Minor:

  • The comment says the second range check (the jump back) "needs a stub larger than 2 GiB to reach". That holds for the 5-byte-or-longer path. The test uses a 3-byte instruction, which takes the other path where the jump back is encoded by the block encoder. The comment is accurate in spirit, but it describes a different branch than the one the test exercises.
  • plan_ranges is followed directly by def build with no blank lines; the file uses two.

Commands run (Linux):

  • git diff --check <merge-base>...HEAD: clean
  • tests/docs_links_check.py: pass
  • tests/bidi_controls_check.py: pass
  • tests/startup_host_cpu_check.py: pass
  • a direct probe of plan_replacement at five site addresses around the limits, and with a 5-byte instruction at near and far sites
  • the four mutations above
  • The merge against current main has no content conflicts (git merge-tree). No Windows run was needed for this Python-only test.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046193823 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1857#pullrequestreview-6046193823). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T19:45:56Z. **Review:** COMMENTED --- Peer review of head `cd2b333c079091dcdc7e863dc556520e1f643505` (PRODUCTION_REVIEW_ONLY, technical comment only). **No P1. One P2: the new test checks "far" but not the limit the title is about.** The change adds only a test (`plan_ranges` in tests/startup_host_cpu_check.py, +23 lines). I read `plan_replacement` in tools/startup/host_cpu.py: both range checks (`forward = stub_address - (site + 5)` and, for sites of 5 bytes or more, `back`) use `-2**31 <= x < 2**31`, which is the correct signed rel32 range. The near case and the far case in the test give the expected results (near: a plan; a site at 0x7FFF00000000 with the stub at 0x1000: refused). What the test does not pin, shown by one-line changes to the `forward` check in tools/startup/host_cpu.py, each run against the check and then restored with `git checkout -- .`: - the check removed (`if False:`): the check fails (rc=1), but through a `struct.error` crash from the unguarded `struct.pack`, not through the new assertion. A version that wrapped the offset instead of crashing would pass. - range widened to `-2**40 <= forward < 2**40`: rc=0, survives. - upper bound made inclusive (`forward <= 2**31`): rc=0, survives. - only non-negative offsets accepted (`0 <= forward < 2**31`): rc=0, survives. The near case places the stub above the site, so a stub below the site is never exercised. The only far input is about 140 TiB away, so any threshold between 2 GiB and that distance passes. I suggest adding the four boundary values: forward = 2**31 - 1 (accepted), 2**31 (refused), -2**31 (accepted), -2**31 - 1 (refused). Placing the stub below the site would also cover the negative sign. For reference, `plan_replacement` at those sites behaves as expected today: for example a site at 0x80001004 with the stub at 0x1000 is refused. Minor: - The comment says the second range check (the jump back) "needs a stub larger than 2 GiB to reach". That holds for the 5-byte-or-longer path. The test uses a 3-byte instruction, which takes the other path where the jump back is encoded by the block encoder. The comment is accurate in spirit, but it describes a different branch than the one the test exercises. - `plan_ranges` is followed directly by `def build` with no blank lines; the file uses two. Commands run (Linux): - `git diff --check <merge-base>...HEAD`: clean - `tests/docs_links_check.py`: pass - `tests/bidi_controls_check.py`: pass - `tests/startup_host_cpu_check.py`: pass - a direct probe of `plan_replacement` at five site addresses around the limits, and with a 5-byte instruction at near and far sites - the four mutations above - The merge against current `main` has no content conflicts (`git merge-tree`). No Windows run was needed for this Python-only test. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-24 21:00:56 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Review of head cd2b333c079091dcdc7e863dc556520e1f643505 (technical comment only, no merge from this pass).

No P1. One P2 (same as the earlier review, reproduced): the new test does not pin the rel32 limit it is about.

The change adds plan_ranges to tests/startup_host_cpu_check.py (+23 lines). Mutations on the forward check at tools/startup/host_cpu.py line 210, each restored with git checkout -- .:

  • check replaced by if False:: rc=1, but only through a struct.error crash from the unguarded struct.pack, not through the new assertion.
  • range widened to -2**40 <= forward < 2**40: rc=0, survives.
  • upper bound made inclusive (forward <= 2**31): rc=0, survives.

The only far input is about 140 TiB away, so any limit between 2 GiB and that distance passes. Suggested fix: assert the four boundary offsets (231 - 1 accepted, 231 refused, -231 accepted, -231 - 1 refused), with the stub both above and below the site.

Commands run (Linux, iced-x86 supplied from a temporary directory outside the repo):

  • git diff --check origin/main...HEAD: rc=0
  • python3 tests/docs_links_check.py: rc=0
  • python3 tests/bidi_controls_check.py: rc=0
  • python3 tests/startup_host_cpu_check.py: rc=0 (PASS)
  • three mutations above: rc=1, rc=0, rc=0

No receipt and no merge: the test kills no real logic mutation through its own assertion.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046868919 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1857#pullrequestreview-6046868919). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T20:29:38Z. **Review:** COMMENTED --- Review of head `cd2b333c079091dcdc7e863dc556520e1f643505` (technical comment only, no merge from this pass). **No P1. One P2 (same as the earlier review, reproduced): the new test does not pin the rel32 limit it is about.** The change adds `plan_ranges` to tests/startup_host_cpu_check.py (+23 lines). Mutations on the `forward` check at tools/startup/host_cpu.py line 210, each restored with `git checkout -- .`: - check replaced by `if False:`: rc=1, but only through a `struct.error` crash from the unguarded `struct.pack`, not through the new assertion. - range widened to `-2**40 <= forward < 2**40`: rc=0, survives. - upper bound made inclusive (`forward <= 2**31`): rc=0, survives. The only far input is about 140 TiB away, so any limit between 2 GiB and that distance passes. Suggested fix: assert the four boundary offsets (2**31 - 1 accepted, 2**31 refused, -2**31 accepted, -2**31 - 1 refused), with the stub both above and below the site. Commands run (Linux, iced-x86 supplied from a temporary directory outside the repo): - `git diff --check origin/main...HEAD`: rc=0 - `python3 tests/docs_links_check.py`: rc=0 - `python3 tests/bidi_controls_check.py`: rc=0 - `python3 tests/startup_host_cpu_check.py`: rc=0 (PASS) - three mutations above: rc=1, rc=0, rc=0 No receipt and no merge: the test kills no real logic mutation through its own assertion. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-24 21:27:23 +00:00
tedin7 commented 2026-11-24 21:27:24 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:09:58Z.

Review: COMMENTED


Review of head cd2b333c07 by ChazY11. Tier B tests: one file touched, tests/startup_host_cpu_check.py. No C++ and no CMakeLists.txt. 0 P1, 0 P2.

Commands executed locally:

  • git diff --check origin/main...HEAD: exit 0
  • python3 tests/docs_links_check.py: self-test passed, 18 Markdown files, 370 links
  • python3 tests/bidi_controls_check.py: self-test passed, 1324 files, no bidirectional controls
  • python3 tests/startup_host_cpu_check.py in a venv with iced-x86==1.21.0 (the pin in requirements-dev.txt): PASS
  • Mutation 1: removed the forward range check in plan_replacement (tools/startup/host_cpu.py). The test now fails with struct.error from the far-site call. Killed. Reverted.
  • Mutation 2: changed the forward check to -2**31 <= forward < 0. The test now fails with "plan_replacement refused a near site". Killed. Reverted.
  • GitHub CI: Build and test on Windows = SUCCESS, Python checks on Linux = SUCCESS, gfx1013 fixtures = SUCCESS.

Notes, not blocking:

  • Widening the bound to 240 survives, because the far site (about 247 away) is still refused. The test guards "far sites are refused", not the exact 2 GiB edge. That is the stated scope.
  • The comment says the second (jump-back) check is unreachable without a stub larger than 2 GiB. That holds for the moved-instruction path (length < 5). With length >= 5 the jump-back check runs first, so a far site there hits it. Wording only.
  • Nit: PEP 8 wants two blank lines between plan_ranges and build.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6047457357 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1857#pullrequestreview-6047457357). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T21:09:58Z. **Review:** COMMENTED --- Review of head cd2b333c079091dcdc7e863dc556520e1f643505 by ChazY11. Tier B tests: one file touched, tests/startup_host_cpu_check.py. No C++ and no CMakeLists.txt. 0 P1, 0 P2. Commands executed locally: - `git diff --check origin/main...HEAD`: exit 0 - `python3 tests/docs_links_check.py`: self-test passed, 18 Markdown files, 370 links - `python3 tests/bidi_controls_check.py`: self-test passed, 1324 files, no bidirectional controls - `python3 tests/startup_host_cpu_check.py` in a venv with iced-x86==1.21.0 (the pin in requirements-dev.txt): PASS - Mutation 1: removed the forward range check in plan_replacement (tools/startup/host_cpu.py). The test now fails with struct.error from the far-site call. Killed. Reverted. - Mutation 2: changed the forward check to `-2**31 <= forward < 0`. The test now fails with "plan_replacement refused a near site". Killed. Reverted. - GitHub CI: Build and test on Windows = SUCCESS, Python checks on Linux = SUCCESS, gfx1013 fixtures = SUCCESS. Notes, not blocking: - Widening the bound to 2**40 survives, because the far site (about 2**47 away) is still refused. The test guards "far sites are refused", not the exact 2 GiB edge. That is the stated scope. - The comment says the second (jump-back) check is unreachable without a stub larger than 2 GiB. That holds for the moved-instruction path (length < 5). With length >= 5 the jump-back check runs first, so a far site there hits it. Wording only. - Nit: PEP 8 wants two blank lines between plan_ranges and build. AI-assisted; commands listed were executed locally.
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!1850
No description provided.